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

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

    In my opinion this is poor because it could be made much more clear with an if statement:

    if (school != null) {
        myLabel.Text = school.SchoolName;
    }
    else {
        myPanel.Visible = false;
    }
    

    That will certainly avoid using exception handling unnecessarily and make the code's meaning very obvious.

提交回复
热议问题