More Elegant Exception Handling Than Multiple Catch Blocks? [duplicate]

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)
{
    ...
}

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.


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.