Is it “bad” to use try-catch for flow control in .NET?

前端 未结 9 1077
天命终不由人
天命终不由人 2020-11-30 14:51

I just found in a project:

try
{
    myLabel.Text = school.SchoolName;
}
catch
{
    myPanel.Visible = false;
}

I want to talk to the devel

9条回答
  •  半阙折子戏
    2020-11-30 15:32

    I agree with everyone here--it's a horrible idea.

    There are a few cases in Java (I think they are mostly gone now, but there may still be some in external libraries) where you were required to catch an exception for certain "non-exception" cases.

    In general, when writing library code (well, any class actually), avoid using exceptions for ANYTHING that could possibly be avoided. If it's possible that a name field isn't set and that should cause an exception in a write() method, be sure to add an isValid() method so that you don't actually HAVE to catch the exception around the write to know there is a problem.

    (Bad Java code addendum): this "good" programming style virtually eliminates any need for checked exceptions in Java, and checked exceptions in Java are the Suck.

提交回复
热议问题