Blog

The case against catch(Exception ex)

June 15, 2006

You should definitely read this fine article about exception handling. When it comes to exception handling, there is still a lot of confusion amongst us, developers. With all the recent ramblings and hypes about WTF, WCF, WWF, … whatever, why don’t we just start implementing exceptions the right way.

The article defends why there is a rule in FxCop that specifies that it’s not a good thing to catch exceptions of type Exception. I feel that the C# compiler should raise an error when writing something like this:

try
{
	// Do something
}
catch(Exception ex) 
{
	// Rethrow or swallow (doesn't matter, it's pure evil!!)
}

I must admit, even writing this syntax now does make me feel bad.

If a bug in your code manifests itself as an exception (and most of the ‘unchecked’ exceptions are just that), then it’s best to let the application terminate right at the point of failure. When the bug is discovered, you’ll get exactly the diagnostic information you need to fix it quickly, add the appropriate regression tests, and move on. On the other hand, debugging a system which aggressively swallows general exceptions feels like you’re one of the police investigators in “CSI: Miami”. The culprit has left the crime scene and you have to work backwards from the subtle clues in your program’s incorrect behavior.

One should only catch an exception when he can do something about it. When you can’t make up a valid reason to catch an exception, then just let it go. When it kills your application: fine! You discovered a bug that needs your attention, and all the information is right there.

And that’s the principal reason to avoid catch (Exception): it hides bugs in your code. These bugs are far more costly to resolve and since they can go undetected, the quality of your software suffers.

There are just a few cases where using a general exception handler is justified. The one use case scenario I can come up with is an application that uses plugins (potentially third-party). I would consider running the plugin code in a general exception handler in order to prevent malicious code from bringing down my application. Maybe there are some multithreading scenario’s, but I’m not entirely sure so let’s keep it by this one example.

If reading this article does not change your mind and you are still putting in general exception handlers in all of your methods, then just do us all a favor and find yourself another career!

If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at our trainings and workshops or check out the books section. Feel free to reach out at infonull@nullprincipal-itnull.be.

Profile picture of Jan Van Ryswyck

Jan Van Ryswyck

Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Comments

About

Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be