Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

try-catch

I just found in a project:

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

I want to talk to the developer than wrote this, saying that incurring the null exception (because school might theoretically be null, not myLabel) would virtually make the computer beep three times and sleep for two seconds. However, I wonder if I'm misremembering the rule about that. Obviously, this isn't the intended use for try/catch, but is this bad because it defies intention, or bad because of performance considerations? I feel like it's just bad, but I want to say more than "that's really bad".

like image 203
dnord Avatar asked Aug 26 '09 16:08

dnord


People also ask

Is it bad practice to use try catch?

Without a try catch, you run the risk of encountering unhandled exceptions. Try catch statements aren't free in that they come with performance overhead. Like any language feature, try catches can be overused.

Why is it bad to use exceptions for control flow?

The use of exceptions for program flow control hides the programmer's intention, that is why it is considered a bad practice. It is hard to reason about ProcessItem function because you can't say what the possible results are just by looking at its signature.

Is try catch control flow?

Since try-catch statement is not used, the control flow can be easy to figure out because it is handled directly from a parent function rather than from another location(exception catch).

Should I use try catch everywhere C#?

Show activity on this post. No, you should not wrap all of your code in a try-catch.


8 Answers

You should not use exceptions for control flow simply because it is bad design. It doesn't make sense. Exceptions are for exceptional cases, not for normal flow. Performance probably won't be an issue in this situation because for most modern applications on modern hardware, you could throw exceptions all day long and the user wouldn't notice a performance hit. However, if this is a high performance application processing a lot of data or doing a lot of some sort of work, then yes, performance would be a concern.

like image 151
Max Schmeling Avatar answered Sep 29 '22 11:09

Max Schmeling


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.

like image 38
Matt Hamsmith Avatar answered Sep 29 '22 12:09

Matt Hamsmith


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.

like image 38
Stan R. Avatar answered Sep 29 '22 11:09

Stan R.


Exceptions do incur runtime overhead, but it's probably negligible here. There will be a difference running in the debugger, but the built binaries should run at pretty much the same speed.

Tell your developer that any chimp can make code the machine can read. Good code is written for human beings, not machines. If a null exception is the only thing you're worried about, then it's probably a bug in the user's code -- noone should ever try to assign null to anything that way. Use an Assert() statement instead.

like image 33
Billy ONeal Avatar answered Sep 29 '22 11:09

Billy ONeal


You are absolutely right that this is bad. It is bad because it defies intention and because it hurts performance.

I realize there is room for different programming styles, but personally, I think that even though this works, and I can see what the code is attempting to do, it also hurts readability and code clarity, making it that much more difficult for the maintenance programmers to follow. An if statement is much more appropriate here.

like image 35
David Avatar answered Sep 29 '22 10:09

David


Throwing exceptions does have a negative impact on performance, see http://msdn.microsoft.com/en-us/library/ms229009(VS.80).aspx

like image 38
Jaimal Chohan Avatar answered Sep 29 '22 12:09

Jaimal Chohan


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.

like image 41
kemiller2002 Avatar answered Sep 29 '22 12:09

kemiller2002


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.

like image 39
Bill K Avatar answered Sep 29 '22 11:09

Bill K