mercredi 24 mai 2017

C# Invoking Exception TargetSite

I have an application that can sometimes throw an exception, most of these are handled but I want the unhandled ones to be recoverable. So I'm trying to invoke the method that caused the exception by using the exception's targetsite like so:

        Exception ex = Global.ThrownException;

        MethodBase mB = ex.TargetSite;


        try
        {

            mB.Invoke(mB, null);
        }
        catch(Exception exc)
        {
            System.Windows.MessageBox.Show(exc.Message);
        }

I'm doing this to make sure that the exception was a one time error before showing the window to the user again.

The test method (and the exception targetsite) I'm trying to invoke is this:

 public void testMethod()
    {
        throw new System.IO.IOException("test");
    }

When I run this, an exception is thrown with the message "Object does not match target type" but since testMethod doesn't have any parameters this shouldn't happen.

Any ideas?





Aucun commentaire:

Enregistrer un commentaire