0

I have this code which Invokes a MethodInfo:

try
{
     registrator.Method.Invoke(instance, parameters);
}
catch{
    registrator.FailureType = RegistratorFailureType.ExceptionInRegistrator;
    //registrator.Exception = e;
}

The Registrator is just a MethodInfo wrapper, the Method property is the MethodInfo object itself. parameters is and object[] and instance is a correct instance of the Method's declaring type (created with Activator.Create).

The Method looks like this (I was testing exception catching):

class Test : Plugin, ITest
{
    public void Register(IWindow window)
    {
        throw new Exception("Hooah");
    }
}

The problem is: The exception is never caught and the Visual Studio's uncaught exception bubble pops up.

This is in VS 2010 with .NET 4.0

Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114

4 Answers4

2

The problem is not in your code anyway.
In the Debug/Exceptions menu, remove all checks.
It should work.

Behrooz
  • 1,696
  • 2
  • 32
  • 54
  • Oh, thanks, this seems to solve my problem. Just one extra question: is this setting saved in project file or in user VS configuration (I mean, if someone else opens my project on his PC, will the exception be disabled there as well)? – Matěj Zábský Aug 30 '10 at 14:36
  • Ah thanks...thats what i was afraid of, I guess there is no way to circumvent this..? – Matěj Zábský Aug 30 '10 at 19:02
0

Have you tried this?

In Program.cs

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

And a try/catch on the Run method:

try
{
    Application.Run(new Form1());
}
    catch (Exception ex)
{
}
Jérémie Bertrand
  • 3,025
  • 3
  • 44
  • 53
0

The problem is not with the code you're showing.

I tried this:

void Main()
{
    Test instance = new Test();
    object[] parameters = new object[] { null };

    MethodInfo method = typeof(Test).GetMethod("Register");

    try
    {
        method.Invoke(instance, parameters);
    }
    catch
    {
        Console.Out.WriteLine("Exception");
    }
}

interface ITest { }
interface IWindow { }
class Plugin { }

class Test : Plugin, ITest
{
    public void Register(IWindow window)
    {
        throw new Exception("Hooah");
    }
}

It printed "Exception" as expected. You need to show us more code.

If I modify the code like this:

catch(Exception ex)
{
    Console.Out.WriteLine(ex.GetType().Name + ": " + ex.Message);
}

Then I get a TargetInvocationException, where the InnerException property is your thrown Exception.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
0

I think the problem is that you are expecting a specific exception type, maybe IOException or something, but actually MethodInfo.Invoke() will throw a TargetInvocationException:

try
{
     registrator.Method.Invoke(instance, parameters);
}
catch (TargetInvocationException tie)
{
    // replace IOException with the exception type you are expecting
    if (tie.InnerException is IOException)
    {
        registrator.FailureType = RegistratorFailureType.ExceptionInRegistrator;
        registrator.Exception = tie.InnerException;
    }
    else
    {
        // decide what you want to do with all other exceptions — maybe rethrow?
        throw;
        // or maybe unwrap and then throw?
        throw tie.InnerException;
    }
}
Timwi
  • 65,159
  • 33
  • 165
  • 230