Reputation: 1566
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?
Upvotes: 21
Views: 20195
Reputation: 16121
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
}
Upvotes: 42
Reputation: 16981
try
{
//code
}
catch (System.Reflection.TargetInvocationException ex)
{
if (ex.InnerException is SpecificException)
{
//fix
}
else
{
throw ex.InnerException;
}
}
or
try
{
//code
}
catch (System.Reflection.TargetInvocationException ex)
{
SpecificException spExc = ex.InnerException as SpecificException;
if (spExc != null)
{
bla-bla spExc
}
else
{
throw ex.InnerException;
}
}
or
try
{
//code
}
catch (System.Reflection.TargetInvocationException ex)
{
if (ex.InnerException.GetType() == typeof(SpecificException))
{
//fix
}
else
{
throw ex.InnerException;
}
}
Upvotes: -2
Reputation: 34198
Your #2 is definitely an interesting solution!
You do want to be careful though: TargetInvocationException
will typically have been thrown by another component when it first caught InnerException
. If you throw ex.InnerException
you're going to destroy some information it contains (like the stack trace) because you're re-throwing it from a different location.
So of the two you've proposed, I'd definitely suggest going with #1. I'm not aware of an alternative within the structure you've got. However, the InnerException will have been thrown originally elsewhere - it' worth investigating whether there's a more elegant place to handle this failure, closer to where the exception is thrown.
Upvotes: 1