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

前端 未结 9 1062
天命终不由人
天命终不由人 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:45

    I never like using exceptions for flow control. Exceptions are expensive, and it is difficult to determine what the actual flow of a program with exceptions being thrown to reach other places in code. To me this is like using GoTo. This doesn't mean that you should avoid exceptions, but rather an exception should be just that, an exception to what should normally happen in the program.

    I think a worse part of the code, is that it's not even doing anything with the exception. There is no logging or even an explanation as to why the exception is being thrown.

提交回复
热议问题