0

I Use the AppDomain.UnhandledException Event to catch unhandled exceptions. I use a MessageBox.Show() instead of Console.WriteLine in the example;

public static void Main()
{
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

    try
    {
        throw new Exception("1");
    }
    catch (Exception e)
    {
        MessageBox.Show("Catch clause caught : " + e.Message);
    }

    throw new Exception("2");
}

static void MyHandler(object sender, UnhandledExceptionEventArgs args) 
{
    Exception e = (Exception) args.ExceptionObject;
    MessageBox.Show("MyHandler caught : " + e.Message);
}

The example says the output should be:

// The example displays the following output: 
//       Catch clause caught : 1 
//        
//       MyHandler caught : 2 

Instead my ex.Message in the UnhandledExceptionEventHandler returns this:

MyHandler caught : 'The invocation of the constructor on type 'FlashMaps.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9'.

When I expected it to return what the example displays:

MyHandler caught : 2

Thank you for your help.

4
  • 1
    Try to do that in empty project. No one knows what is FlashMaps.MainWindow. Commented Jun 12, 2014 at 20:04
  • @Jack Malkovich it was an empty project, I tried making a new one for peace of mind but it just says the same thing with different project name :( Commented Jun 12, 2014 at 20:07
  • 1
    Let me guess, in your WPF project, the code from your question is actually in the constructor of your main window, correct? Look at the inner exception caught by the UnhandledExceptionEventHandler, then... Commented Jun 12, 2014 at 20:16
  • @elgonzo, that is the correct answer. If you post as answer I will accept. Thanks a lot Commented Jun 12, 2014 at 20:19

2 Answers 2

1

In WPF, if exceptions occur when UIElements (including windows) are being instantiated via XAML, the framework's XAML/BAML engine creates an exception of its own and puts the original exception into its InnerException property.

Thus, to get full exception information, you can do something similar to the following in your exception handler(s):

    try
    {
        ...
    }
    catch (Exception ex)
    {
        string message = "";
        string formatString = "{0}: {1}";
        do
        {
            message += string.Format(formatString, ex.GetType(), ex.Message);
            ex = ex.InnerException;
            formatString = "\n    Inner {0}: {1}";
        }
        while (ex != null);
        MessageBox.Show(message);
    }

The example given here in the form of a catch-block can be applied in the same manner to your UnhandledExceptionEventHandler.

Sign up to request clarification or add additional context in comments.

Comments

0

Your code is not running in full trust. Add the permissions on the method

[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.