More Elegant Exception Handling Than Multiple Catch Blocks?

44,325

Solution 1

In my opinion, a bunch of "ugly" catch blocks IS the best way to handle that situation.

The reason I prefer this is that it is very explicit. You are explicitly stating which exceptions you want to handle, and how they should be handled. Other forms of trying to merge handling into more concise forms lose readability in most cases.

My advice would be to stick to this, and handle the exceptions you wish to handle explicitly, each in their own catch block.

Solution 2

I agree with Reed: this is the best approach.

I would add these comments:

Only catch something you're going to do something about. If you can't fix the problem, there's no point in catching a specific exception.

Don't overdo use of catch blocks. In many cases where you can't resolve the exception, it's best to just let the exception bubble up to a central point (such as Page_Error) and catch it there. Then, you log the exception and display a message to the user.

Solution 3

About the only other thing you can do is emulate VB.NET's exception filters:

try {
    DoSomething();
} catch (Exception e) {
    if (!ex is CustomException && !ex is AnotherCustomException) {
       throw;
    }
    // handle
}

Sometimes this is better, sometimes not. I'd mainly use it if there was some common logic I wanted in the handler, but the exceptions don't share a base type.

Solution 4

Unfortunately, C# does not have user exception filters like VB.NET, so you're limited to:

  1. Catching a common ancestor to all exceptions. This may or may not be what you want, because there may be other descendant exception types that you do not want to catch.
  2. Moving the exception handling logic into another method and calling that from each handler.
  3. Repeating the exception logic for each handler.
  4. Moving the exception handling logic into a language that supports filters, such as VB.NET.

Solution 5

If you need to write a really big ammount of code like this I would suggest checking some AOP framework. I personally use PostSharp. Then you could hide all exception handling code into aspects.

Share:
44,325
Aaron
Author by

Aaron

Updated on July 28, 2022

Comments

  • Aaron
    Aaron almost 2 years

    Using C#, is there a better way to handle multiple types of exceptions rather than a bunch of ugly catch blocks?

    What is considered best practice for this type of situation?

    For example:

    try
    {
        // Many types of exceptions can be thrown
    }
    catch (CustomException ce)
    {
        ...
    }
    catch (AnotherCustomException ace)
    {
        ...
    }
    catch (Exception ex)
    {
        ...
    }
    
  • Reed Copsey
    Reed Copsey about 15 years
    +1 - Great point about not catching "extra" exceptions. I was trying to mention that (in my saying that it causes you to be explicit about what you want to handle) - but you're right - you usually shouldn't handle everything, unless you're going to handle it in a meaningful way.
  • Mike Cole
    Mike Cole about 15 years
    Also +1. I want to add that sometimes developers add catch blocks to every method so they can trace what method actually threw the exception. That's what the stack trace is for!
  • John Saunders
    John Saunders about 15 years
    Not good, in general. Don't suppress exceptions without logging them in some way. Otherwise, you could be hiding important information. Also, IMHO there is very little reason to ever do "catch (exception ex)", unless you're going to log it, or wrap it, and then use the "throw;" statement to allow it to propagate.
  • Ben Aston
    Ben Aston over 14 years
    Won't this swallow the exception whole, which is almost always a bad idea?
  • TrueWill
    TrueWill about 14 years
    Do you see any downsides to Mark Brackett's filter solution?
  • Reed Copsey
    Reed Copsey about 14 years
    @TrueWill: IMO, it just reduces maintainability, without really adding a lot of functionality. That being said, it's not horrible - just non-obvious.
  • Maverick
    Maverick almost 10 years
    Yes, this is an AWFUL piece of code, as-is. I think Mash was implying you throw in there...either that or he was drunk.
  • Craig Tullis
    Craig Tullis over 9 years
    Negative logic makes my left eye feel funny. In a case like this, how about if(!(ex is CustomException || ex is AnotherCustomException)) ?
  • Tim Sparkles
    Tim Sparkles over 9 years
    That's still negative logic, you just ran De Morgan on it. Readability is arguably bad either way. I would do something like if (ex is CustomException || ex is AnotherCustomException) { /*handle*/ } else { throw; } or bool canHandle = ex is CustomException || ex is AnotherCustomException; if (!canHandle) throw; /* handle */
  • qJake
    qJake almost 9 years
    As of the C# 6.0 language specification (new in .NET 4.6 / Visual Studio 2015), C# supports "Exception Filters" using the syntax: catch (Exception ex) if (ex.Message.Contains("Fatal")).