Only catch TargetInvocationException's - #211
Conversation
|
@henkmollema, |
|
I actually ran into this when attempting to activate an abstract type. I proposed a more specific fix for that in #210. However, there might be other scenarios where the inner exception is null. |
|
@pakrym - can you take a look? |
| return _constructor.Invoke(_parameterValues); | ||
| } | ||
| catch (Exception ex) | ||
| catch (Exception ex) when (ex.InnerException != null) |
There was a problem hiding this comment.
Maybe we should just catch different exception type? Like TargetInvocationException?
There was a problem hiding this comment.
Yeah, that would work as well. There are some other exceptions that Invoke might throw though. Do you think it's safe to assume it will just throw a TargetInvocationException?
There was a problem hiding this comment.
Invoke specifically throws TargetInvocationException when it was thrown by the method that's called. The other exception types indicate a mistake on the part of the code doing reflection
There was a problem hiding this comment.
Yes, of course. Not sure what I was thinking 😅
I'll change the catch-clause.
|
And I would prefer having a test is possible. |
When a `TargetInvocationException` occurs, we want to throw its inner exception. Other exception types are not guaranteed to have an inner exception.
|
@henkmollema thank you! |
There might be cases when the inner exception is null. This results in another exception from
ExceptionDispatchInfo.Capturebecause thesourceparameter isnull.