try- catch. Handling multiple exceptions the same way (or with a fall through)

28,690

Solution 1

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

Alternatively you could do as explained in this question:

Catch multiple Exceptions at once?

Personally I tend to prefer the method-based approach.

Solution 2

You should really have a BaseCustomException and catch that.

Solution 3

This is copied from another posting, but I am pulling the code to this 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 this to repeating a method call in several catch blocks.

Solution 4

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

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

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

Solution 5

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

Share:
28,690

Related videos on Youtube

ram
Author by

ram

Updated on July 09, 2022

Comments

  • ram
    ram almost 2 years

    There has already been a question posted here which is very similar. Mine is extending that question a bit more. Say you want to catch multiple types of exception but want to handle it the same way, is there a way to do something like switch case ?

    switch (case)
    {
      case 1:
      case 2:
    
      DoSomething();
      break;
      case 3:
      DoSomethingElse()
      break;
    
    }
    

    Is it possible to handle few exceptions the same way . Something like

    try
    {
    }
    catch (CustomException ce)
    catch (AnotherCustomException ce)
    {
      //basically do the same thing for these 2 kinds of exception
      LogException();
    }
    catch (SomeOtherException ex)
    {
     //Do Something else
    }
    
  • Alison R.
    Alison R. about 14 years
    This might work, but this is not a good solution, since it ignores two very fundamental facts: a) there is a base system Exception class, and you don't need to go all the way down to System.Object; and b) multiple catch statements for different exception classes exist for this very purpose.
  • torak
    torak about 14 years
    I agree with Allison R on point (a), but in relation to point (b) it seems like criticism for answering the question that was asked.
  • Alison R.
    Alison R. about 14 years
    Breaking out common functionality into a method and calling it in various catch() blocks is the more elegant way to go, since it doesn't require type checking, and makes proper use of native language constructs for exception handling.
  • Jacob Brewer
    Jacob Brewer almost 11 years
    To copy the data to this thread: catch (Exception ex) { if (ex is FormatException || ex is OverflowException) { WebId = Guid.Empty; return; } throw; }
  • santa
    santa about 10 years
    sidenote: the "throw obj;" in the very end should better either be something like only "throw;" or "throw new FooException("blabla", obj);" i guess
  • santa
    santa about 10 years
    i should probably add a reason to state so too: to preserve the stacktrace inside 'obj' :)
  • Gusdor
    Gusdor almost 9 years
    Why not? There may be 3 custom libraries in effect within the try catch block.
  • Stan R.
    Stan R. almost 9 years
    you're right, in general its perfectly fine to catch as many custom exceptions as you need, in fact its preferred, if you handle them all differently.