Empty Catch Blocks

I’ve just found 22 examples of this anti-pattern in a small .NET project:

emptycatchblock

Oh yes, the empty catch block. Joy of joys.

Declaring an empty catch block is equivalent to professing that:

"Sometimes this code I’ve written fails. I don’t know why. I don’t really care. Please don’t tell me when it happens. I can’t hear you. La la la…"

*sigh*

If I ruled the world, empty catch blocks would be invalid in C#. Is there ever any reason for using them?

#1  avatar Jack Hughes on 11 Jun 2009 at 13:43

I agree that empty catch blocks are pretty ugly, but not having an exception contract in the signature of a method in c# (as Java does for instance) doesn't help either.

#2 ian avatar Ian Nelson on 11 Jun 2009 at 13:50

Hi Jack,

The ugliness I can cope with, it's the fact that they swallow any issues which could possibly occur within the try block that I object to.

.NET 4.0 brings Code Contracts to the party (not in the method signature but in the form of preconditions, postconditions, and object invariants). ianfnelson.com/.../code-contracts

#3  avatar Tobin Harris on 11 Jun 2009 at 14:09

This could be called "Silent Fail". It's a deadly anti-pattern, known to cause hair loss and premature aging. It's rife in COM development projects :)

#4  avatar Tobin Harris on 11 Jun 2009 at 14:11

Whilst I'm writing twoddle..

I think I recall Bill Gates saying "Bad news should travel faster than good news.", this is like saying "Bad news should be ignored."

#5 ian avatar Ian Nelson on 11 Jun 2009 at 14:17

Even when exceptions *are* thrown, there's a danger that they merely get written to the NT Event Log, and go uninvestigated until a user complains.

I would like to think that the increasing popularity of ELMAH and Exceptioneer will make it more fashionable to get exceptions in the face of the developers, so they can proactively fix the problems.

And on the subject of Bill's quote, I'm reminded of the bit in Hitchhiker's Guide where the Hingefreel people native to Arkintoofle Minor constructed a starship powered by bad news. Unfortunately, the ship's drives didn't work terribly well, and, even if they did, their fuel source rendered them incredibly unwelcome, thus defeating the whole point. :)

#6  avatar Mark Smith on 11 Jun 2009 at 14:42

God, I hate these. Empty exception handlers are one of the very few sackable offences.

Here's an amusing variant of crap exception handling and usage I've seen this week:

An app that regularly throws a custom exception when you call SomeClass.Initialize() more than once. The app code is liberally sprinkled with calls to SomeClass.Initialize(), all of them with an associated /empty/ exception handler for that exception class.

SomeClass.Initialize() also throws the same exception when something really has gone wrong. Of course, the empty catch blocks immediately swallow the useful exception.

#7  avatar John Topley on 11 Jun 2009 at 15:05

I know very little about C#. Does it force you to catch checked exceptions like Java does?

#8 ian avatar Ian Nelson on 11 Jun 2009 at 15:10

@John - C# does not support checked exceptions.

As such, it is not always obvious what exceptions could be thrown by any piece of code, which I suppose might be responsible for lazy devs writing empty catch blocks.

Here's a C#/Java exceptions comparison - msdn.microsoft.com/.../ms228505.aspx

#9 ian avatar Ian Nelson on 11 Jun 2009 at 15:11

Rory Becker has just told me about this plugin for CodeRush to help with this problem:

code.google.com/.../CR_ExceptionHel

"This plugin provides a single action "Add Exception Handlers" which, when bound to a key and activated, will present you with a list of exceptions which might be thrown by the currently selected code. Select the ones you want to handle and hit 'OK'. A try..catch will be generated around your selected code."

Leave a Comment