开发者

Can I execute multiple Catch blocks?

This is a bit abstract, but is there any possible way to throw an exception and have it enter multiple catch blocks? For example, if it matches a specific exception followed by a non-specific exception.

catch(Arithmetic exception开发者_JAVA百科)
{
  //do stuff
}
catch(Exception exception)
{
  //do stuff
}


It is perfectly acceptable to have multiple catch blocks of differring types. However, the behavior is that the first candidate block handles the exception.

It will not enter BOTH catch blocks. The first catch block that matches the exception type will handle that specific exception, and no others, even if it's rethrown in the handler. Any subsequent ones will be skipped once an exception enters a catch block.

In order to have an exception caught in BOTH blocks, you would need to either nest blocks like so:

try
{
     try
     {
        // Do something that throws  ArithmeticException
     }
     catch(ArithmeticException arithException)
     {
        // This handles the thrown exception....

        throw;  // Rethrow so the outer handler sees it too
     }
}
catch (Exception e)
{
   // This gets hit as well, now, since the "inner" block rethrew the exception
}

Alternatively, you could filter in a generic exception handler based on the specific type of exception.


No. It isn't possible to execute the code in both catch blocks for a single exception.

I would probably refactor the code in the generic exception block into something that can be called from either.

try
{
   // blah blah blah
{
catch(Arithmetic ae)
{
   HandleArithmeticException( ae );
   HandleGenericException( ae );
}
catch(Exception e)
{
  HandleGenericException( e );
}


Like others said the exception will be caught by the most specific catch block.

This brings up a frustration of mine though with exception handling. I wish you could do something like

catch (ArgumentNullExcpetion, ArugmentOutOfRangeException ex)
{

}

Instead of having to do

catch (ArgumentNullExcpetion e)
{
}
catch (ArugmentOutOfRangeException outOfRange)
{
}

I understand the reasoning against this that you probably do different things for different exceptions but sometimes I want combine them.


You can't have more than one exception block handle the same exception. But what you can do is catch the general exception, then attempt to cast to the more specific, like this:

catch (Exception exception)
{
    var aex = exception as ArithmeticException
    if (aex != null)
    {
        // do stuff specific to this exception type
    }
  // then do general stuff
}


If you were using VB.NET you could abstract your error handler in the Arithmetic exception into a function or method call that always returns false.

Then you could write something like:

Catch ex as Arithmetic When HandleArithmetic()
Catch ex as Exception

End Try

Not that I would advocate such usage, though I have seen it recommended for logging purposes before. I don't believe there is a C# equivalent.


This is known as exception filtering and isn't supported in C# (I'm told it is possible in VB.NET).

One work around would be to catch the general exception and then check the exception type in the catch block and do any specific processing on that before carrying on with the rest of the block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜