I have a call to MethodInfo.Invoke()
to execute a function through reflection. The call is wrapped in a try/catch
block but it still won't catch the exception thrown by the function I'm invoking.
I receive the following message:
Exception was unhandled by the user.
Why does MethodInfo.Invoke()
prevent the Exception to be caught outside of the Invoke()
?
How do I bypass it?
EDIT: As I understand your issue, the problem is purely an IDE one; you don't like VS treating the exception thrown by the invocation of the
MethodInfo
as uncaught, when it clearly isn't. You can read about how to resolve this problem here: Why is TargetInvocationException treated as uncaught by the IDE? It appears to be a bug / by design; but one way or another, decent workarounds are listed in that answer.As I see it, you have a couple of options:
You can use
MethodInfo.Invoke
, catch theTargetInvocationException
and inspect itsInnerException
property. You will have to workaround the IDE issues as mentioned in that answer.You can create an appropriate
Delegate
out of theMethodInfo
and invoke that instead. With this technique, the thrown exception will not be wrapped. Additionally, this approach does seem to play nicely with the debugger; I don't get any "Uncaught exception" pop-ups.Here's an example that highlights both approaches:
How are you trying to catch the exception? Typically what is thrown from a call to
Invoke()
is a wrapping exception instance ofSystem.Reflection.TargetInvocationException
. The actual exception you're after will be in theInnerException
.