3

What is the best way to exclude an exception type from your catch? You may not know what types of exceptions are coming in so one of your catch may be the generic catch(Exception ex) , and you could easily check the type of that exception and if it matches the one you want to exclude, then throw it back up, but im guessing that is very inefficient. Is there a better way to do it?

Chris
  • 968
  • 16
  • 27
  • 2
    _"but im guessing that is very inefficient"_ exceptions are for exceptional problems not for program's normal flow. So performance should be the last thing to consider. – Tim Schmelter Aug 15 '14 at 15:07
  • 1
    You can have multiple catch clauses for different exceptions. If that doesn't work because you really want that "catch-all" then you should indeed rethrow it. If we're talking about the future, then you can also use [exception filters](http://msdn.microsoft.com/en-us/magazine/dn683793.aspx) in C# 6 which will work like this: `try { } catch (Exception ex) if (typeof(ex) != typeof(StupidException)) { }` – Jeroen Vannevel Aug 15 '14 at 15:08
  • 1
    @JeroenVannevel I don't think that would be valid even in C# 6, because `typeof()` applies to types, not objects. I think it would be `catch (Exception ex) if (!(ex is StupidException)) { }` – cdhowie Aug 15 '14 at 15:32
  • @cdhowie: you're entirely right, ofcourse. I mixed it up with `.GetType()` but `is` works as well indeed. – Jeroen Vannevel Aug 15 '14 at 15:33
  • _"You may not know what types of exceptions are coming in"_ Any place you are catching exceptions, you should know what exceptions to expect and catch. Anything else should be allowed to bubble up. – Chris Dunaway Aug 15 '14 at 18:05

3 Answers3

9

The most straightforward way would be to have a block for the kind of exception you don't want to catch:

try {
    // ....
} catch (DoNotWantToCatchException) {
    throw;
} catch (Exception ex) {
    // Handle exception
}

There isn't any simpler way to accomplish your requirement.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 4
    As of C# 6.0 it is now possible to conditionally exclude with a "with" clause: ```try { } catch (Exception ex) when (!(ex is ExcludedException)) { /* do stuff */ }``` – Kelmar Oct 09 '19 at 21:17
  • 2
    @Kelmar, you should add this as an answer – Motti Jul 07 '20 at 13:23
2

Very strange requirement. But you can catch this particular exception type and re-throw it

try
{
   // code
}
catch(YourSpecificException e)
{
   throw;
}
// catch other exceptions here (which you want to handle)
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • @downvoters if you have better solution, then feel free to post it or at least explain whats wrong with re-throwing – Sergey Berezovskiy Aug 15 '14 at 15:12
  • In the .NET exception model, exception filters between the place an exception is thrown and the place it is caught are supposed to run before `finally` handlers. Adding a `catch` and rethrow will cause `finally` clauses within the `try` block to run before any outer exception filters get a chance to do so. That is ugly, but until the next version of C# comes out with exception-filter support, I don't know any better approach in C#. – supercat Aug 15 '14 at 16:42
0

create a separate catch for it eg:

try
{
    //do stuff
}
catch (exception to exclude ex)
{
    //do stuff
}
catch (Exception ex)
{
    //do stuff
}
DotN3TDev
  • 138
  • 9