9

When calling a method via methodInfo.Invoke, if an exception is thrown it does not seem to propagate up to my catch blocks.

object value;
try
{
    value = myMethod.Invoke(null, parameters);//program crashes with uncaught exception
}
catch
{
    throw new Exception("Caught!");//never executed
}

The particular exception this method is raising is KeyNotFoundException, but that shouldn't matter because I'm catching everything right?

The particular error message I get from Visual Studio is

KeyNotFoundException was unhandled by user code

whereas normally the message would say

KeyNotFoundException was unhandled

if the call was not a reflected invocation.

I could just have the method check to see if they key is in there, and if not return null, but Using exception handling seems preferable. Is there any way to propagate exceptions up from a reflected method call?

Lucina
  • 490
  • 4
  • 16
  • Hi Lucina, I'm just hijacking this post to let you know that on TeX.SX, it's usually better to self-answer than to delete [your question](http://tex.stackexchange.com/q/255629/17423). I was very curious about what kaomoji would look like typeset by TeX :) and I'm sure others were as well. I'll delete this comment in a day or so (or as soon as you let me know you've seen it) -- I just wanted to let you know. :) – Sean Allred Jul 16 '15 at 14:51

3 Answers3

3

It works for me:

using System;
using System.Reflection;

class Program
{
    static void Main(string[] args)  
    {
        var method = typeof(Program).GetMethod("ThrowException");
        try
        {
            method.Invoke(null, null);
        }
        catch (TargetInvocationException e)
        {
            Console.WriteLine("Caught exception: {0}", e.InnerException);
        }
    }

    public static void ThrowException()
    {
        throw new Exception("Bang!");
    }
}

Note that you do need to catch TargetInvocationException which is the exception thrown directly by Method.Invoke, wrapping the exception thrown by the method itself.

Can you come up with a similar short but complete program which demonstrates the problem?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

This could be an issue with the Visual Studio debugger as well. As noted in the accepted answer to this similar question here, there are a few workarounds that you can do. The simplest of which is changing your Visual Studio debugger to turn off "Just My Code" in Tools -> Options -> Debugging -> General. You can also wrap it in a delegate or explicitly try to catch the invocation exception and inspect the inner exception of that, which should be your KeyNotFoundException.

Community
  • 1
  • 1
Evan Phillips
  • 261
  • 2
  • 4
1

If an error occurs during a method invoked with reflection, it should throw a TargetInvocationException that wraps (via .InnerException) the original. There are, however, a few methods that could cause more terminal fails, such as a few methods around winform creation / the message loop.

It is also possible that the method is working, but is causing additional work to happen on another thread, and it is that that is failing. This would kill the thread, and you can't catch it as it isn't on your thread. This would be particularly likely if your code is actually on a worker thread.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900