[ACCEPTED]-try- catch. Handling multiple exceptions the same way (or with a fall through)-error-handling

Accepted answer
Score: 18

Currently there is no language construct 7 to accomplish what you want. Unless the 6 exception all derive from a base exception 5 you need to consider refactoring the common 4 logic to a method and call it from the different 3 exception handlers.

Alternatively you could 2 do as explained in this question:

Catch multiple Exceptions at once?

Personally 1 I tend to prefer the method-based approach.

Score: 9

You should really have a BaseCustomException 1 and catch that.

Score: 5

This is copied from another posting, but I am pulling the code to this 3 thread:

Catch System.Exception and switch on the types

catch (Exception ex)            
{                
    if (ex is FormatException || ex is OverflowException)
    {
        WebId = Guid.Empty;
        return;
    }

    throw;
}

I prefer 2 this to repeating a method call in several 1 catch blocks.

Score: 2

In vb.net, one can use exception filters 4 to say, e.g.

  Catch Ex As Exception When TypeOf Ex is ThisException Or TypeOf Ex is ThatException

Unfortunately, for whatever 3 reasons, the implementors of C# have as 2 yet refused to allow exception filtering 1 code to be written within C#.

Score: 1

You shouldn't be catching this many custom 2 exceptions,however if you want you can create 1 a common BaseException and catch that.

More Related questions