Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the type of an inner exception

Tags:

c#

In my code I'm coming across a situation in which a System.Reflection.TargetInvocationException is thrown. In one specific case I know how I want to handle the root exception, but I want to throw all other exceptions. I can think of two ways of doing this, but I'm not sure which is better.

1.

try
{
    //code
}
catch (System.Reflection.TargetInvocationException ex)
{
    if (typeof(ex.InnerException) == typeof(SpecificException))
    {
        //fix
    }
    else
    {
        throw ex.Innerexception;
    }
}

2.

try
{
    //code
}
catch (System.Reflection.TargetInvocationException ex)
{
    try
    {
        throw ex.InnerException;
    }
    catch (SpecificException exSpecific)
    {
        //fix
    }
}

I'm aware that throwing exceptions in general is slow, so I feel the first method would possibly be faster. Alternatively, is there a better way of doing this that I haven't thought of?

like image 558
geekchic Avatar asked Apr 12 '12 09:04

geekchic


People also ask

How do I find inner exception details?

When you're debugging and you get an exception, always, always, always click on the View Details link to open the View Details dialog. If the message in the dialog isn't expanded, expand it, then scan down to the Inner Exception entry.

What is inner exception?

Exception. An object that describes the error that caused the current exception. The InnerException property returns the same value as was passed into the Exception(String, Exception) constructor, or null if the inner exception value was not supplied to the constructor. This property is read-only.

What is inner exception in Java?

You can set the inner exception (AKA the cause) in two ways. If you're instantiating the exception yourself, pass the inner exception to the (outer) exception's constructor, e.g. try { // some code that throws innerException } catch (Exception innerException) { throw new OuterException(innerException); }


1 Answers

Each of your proposed solutions has its own issue.

The first method checks that the type of the inner exception is exactly the type you're expected. That means that a derived type won't match, which might not be what you intended.

The second method overwrites the inner exception's stack trace with the current stack location, as Dan Puzey mentioned. Destroying the stack trace may be destroying the one lead you require in order to fix a bug.

The solution is basically what DarkGray posted, with Nick's suggestion and with an added suggestion of my own (in the else):

try 
{ 
    // Do something
} 
catch (TargetInvocationException ex) 
{ 
    if (ex.InnerException is SpecificException) 
    { 
        // Handle SpecificException
    }
    else if (ex.InnerException is SomeOtherSpecificException)
    {
        // Handle SomeOtherSpecificException
    }
    else 
    { 
        throw;    // Always rethrow exceptions you don't know how to handle.
    } 
}

If you want to re-throw an exception that turns out you can't handle, don't throw ex; since that will overwrite the stack trace. Instead use throw; which preserves the stack trace. It basically means "I actually didn't want to enter this catch clause, pretend I never caught the exception".

Update: C# 6.0 offers a much better syntax via Exception Filters:

try
{
    // Do something
}
catch (TargetInvocationException ex) when (ex.InnerException is SpecificException)
{
    // Handle SpecificException
}
catch (TargetInvocationException ex) when (ex.InnerException is SomeOtherSpecificException)
{
    // Handle SomeOtherSpecificException
}
like image 108
Allon Guralnek Avatar answered Sep 21 '22 19:09

Allon Guralnek