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
}
Community
  • 1
  • 1
ram
  • 11,468
  • 16
  • 63
  • 89
  • possible duplicate of [Catch multiple Exceptions at once?](http://stackoverflow.com/questions/136035/catch-multiple-exceptions-at-once) – nawfal May 18 '13 at 11:08

6 Answers6

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.

Community
  • 1
  • 1
João Angelo
  • 56,552
  • 12
  • 145
  • 147
  • 2
    To copy the data to this thread: `catch (Exception ex) { if (ex is FormatException || ex is OverflowException) { WebId = Guid.Empty; return; } throw; }` – Jacob Brewer Jul 12 '13 at 17:17
9

You should really have a BaseCustomException and catch that.

pdr
  • 6,372
  • 1
  • 29
  • 38
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.

Community
  • 1
  • 1
Jacob Brewer
  • 2,574
  • 1
  • 22
  • 25
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#.

supercat
  • 77,689
  • 9
  • 166
  • 211
1

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

Stan R.
  • 15,757
  • 4
  • 50
  • 58
  • Why not? There may be 3 custom libraries in effect within the try catch block. – Gusdor May 27 '15 at 07: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 '15 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;
  }
}
torak
  • 5,684
  • 21
  • 25
  • 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 '10 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 '10 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 '10 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 '14 at 12:37
  • i should probably add a reason to state so too: to preserve the stacktrace inside 'obj' :) – santa Mar 24 '14 at 15:53