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

前端 未结 9 1081
天命终不由人
天命终不由人 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 think this is bad because it is coding against an exception for one and it will also inherit unnecessary overhead. Exceptions should only be caught if they are going to be handled in a specific way.

    Exceptions should be caught specifically for Exceptional cases that you can not predict, in this case it is a simple check to see if school can be null, in fact it is expected that school might be null (since the label is set nothing). If school was null and it should not have been than it should throw its own ArgumentNullException.

提交回复
热议问题