HuBeZa
HuBeZa

Reputation: 4761

Better way to ignore exception type: multiple catch block vs. type querying

There are situations that we like to ignore a specific exception type (commonly ObjectDisposedException). It can be achieved with those two methods:

try
{
    // code that throws error here:
}
catch (SpecificException) { /*ignore this*/ }
catch (Exception ex)
{
    // Handle exception, write to log...
}

or

try
{
    // code that throws error here:
}
catch (Exception ex)
{
    if (ex is SpecificException) { /*ignore this*/ }
    else
    {
        // Handle exception, write to log...
    }
}

What are the pros and cons of this two methods (regarding performance, readability, etc.)?

Upvotes: 2

Views: 4053

Answers (6)

tdammers
tdammers

Reputation: 20721

I'd say it's a matter of preference mainly, but the dedicated emtpy catch looks cleaner to me. Programmers reading your code can be assumed to be familiar with try/catch constructs, and they expect you to sort your catch blocks from specific to general. The usual way people read try/catch constructs is to skim through the catches until they find one that matches what they're looking for (just like the compiler does), and then see what it does. The reader is doing this anyway, so when you have a dedicated empty catch for that exception type, it is immediately obvious that it is a special case and that you're discarding the exception. The built-in type checking logic, OTOH, requires the reader to find the more general exception branch, which is probably way down the list of catch blocks, and then read through the actual logic to find out what happens. I'd say this requires far more reading effort than an empty catch.

Another point is that you're supposed to have a good reason for ignoring exceptions; with an empty catch block, it will be immediately obvious to you and anyone who reviews your code that you are ignoring errors, which is good - it means people will notice and be a bit more conscious about such a potential gotcha, and you'll be pushed toward adding a comment that explains why you're doing it. If you bury the ignore-this-exception part inside the handler logic, people might read past it and then wonder why their exception doesn't pop up anywhere.

Upvotes: 6

mgronber
mgronber

Reputation: 3419

I would prefer the first style as it is more readable and it suits better when you make different decisions for different exception types.

try {
    // ...
} catch (IgnoreThisException) {
    // ignore this
} catch (UnableToHandleHereException) {
    throw; // this should be catched by caller
} catch (Exception e) {
    // log the rest
}

Upvotes: 2

Matthew Flaschen
Matthew Flaschen

Reputation: 284927

I think the first is clearly better. It is the normal method for catching multiple exceptions provided by the language. It also maps directly onto two catch blocks in the CIL, while the second requires an isinst.

I don't know whether this will concretely improve performance, but it should be easier for the JIT to optimize.

Upvotes: 1

Eden
Eden

Reputation: 4206

I would use something along the lines of the second style but will encapsulate the actual logic in another method:

try
{
    // code that throws error here:
}
catch (Exception ex)
{
    ExceptionHandling.Handle(ex);
}

And internally would check for the type

if (ex is SpecificException) { /*ignore this*/ }
else
{
        // Handle exception, write to log...
}
  1. IMO it is more readable.
  2. You don't repeat the handling logic
  3. If you will ever need to handle ObjectDisposedException it would be easy enough to change

Thanks, E

Upvotes: 2

Richard
Richard

Reputation: 109120

catch (SpecificException) { /*ignore this*/ }

is about as explicit as one can get—but only if you include the comment to ensure it is clear this isn't a case of forgetting to fill in the handling code.

Upvotes: 3

tenor
tenor

Reputation: 1105

The second style is better because you are ignoring an exception. It communicates intent better than the first style.

If another developer saw the first style, he/she will wonder if you forgot to write code that handles the ignored exception.

In terms of performance, there will be no noticeable difference.

Upvotes: 0

Related Questions