Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to exclude an exception type from your catch? [duplicate]

Tags:

c#

exception

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?

like image 358
Chris Avatar asked Oct 27 '25 09:10

Chris


2 Answers

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.

like image 91
cdhowie Avatar answered Oct 29 '25 22:10

cdhowie


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)
like image 34
Sergey Berezovskiy Avatar answered Oct 30 '25 00:10

Sergey Berezovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!