27

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
}
1

6 Answers 6

19

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.

1
  • 2
    To copy the data to this thread: catch (Exception ex) { if (ex is FormatException || ex is OverflowException) { WebId = Guid.Empty; return; } throw; } Jul 12, 2013 at 17:17
9

You should really have a BaseCustomException and catch that.

6

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.

2

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#.

1

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

2
  • Why not? There may be 3 custom libraries in effect within the try catch block.
    – Gusdor
    May 27, 2015 at 7:18
  • 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.
    – Stan R.
    May 27, 2015 at 18:44
-5

I've never actually done this or anything like it, and I don't have access to a compiler for testing purposes but surely something like this would work. Not sure how to actually do the type comparison or if C# would let you replace the if statements with a case statement.

try 
{ 
}
catch (System.Object obj)
{
  Type type;

  type = obj.GetType() ;
  if (type == CustomException || type == AnotherCustomException)
  { 
    //basically do the same thing for these 2 kinds of exception 
    LogException(); 
  } 
  else if  (type == SomeOtherException ex) 
  { 
    //Do Something else 
  }
  else
  {
    // Wasn't an exception to handle here
    throw obj;
  }
}
5
  • 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.
    – Alison R.
    Feb 18, 2010 at 19:48
  • 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.
    – torak
    Feb 18, 2010 at 20:00
  • 2
    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.
    – Alison R.
    Feb 18, 2010 at 20:05
  • 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
    Mar 24, 2014 at 12:37
  • i should probably add a reason to state so too: to preserve the stacktrace inside 'obj' :)
    – santa
    Mar 24, 2014 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.