8

I'm working on the UWP application. And I'm using async/await a lot. Always when an exception occurs in my code the debugger set break in App.g.i.cs

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
            };
#endif

But I want to see the line when the exception occured. How to achieve that behavior ?

6
  • You would have to handle the exception where it is thrown. For instance a try catch? Not sure what answer you are looking for here Commented May 20, 2016 at 14:33
  • May be that is XAML exception. Commented May 20, 2016 at 14:38
  • 1
    @Default, the problem is I don't know where it is thrown and I want debuger to show it for me )) Commented May 20, 2016 at 15:30
  • @Janak, no, NullReferenceException for examle, in a view model Commented May 20, 2016 at 15:30
  • Possible duplicate of Visual Studio: How to break on handled exceptions? Commented May 20, 2016 at 16:04

1 Answer 1

9

Add the following method to your App class:

private static void UnhandledError(object sender, UnhandledErrorDetectedEventArgs eventArgs)
{
    try
    {
        // A breakpoint here is generally uninformative
        eventArgs.UnhandledError.Propagate();
    }
    catch (Exception e)
    {
        // Set a breakpoint here:
        Debug.WriteLine("Error: {0}", e);
        throw;
    }
}

In your App constructor:

public UnitTestApp()
{
    CoreApplication.UnhandledErrorDetected += UnhandledError;

    // ...and any other initialization.
    // The InitializeComponent call just sets up error handlers,
    // and you can probably do without in the case of the App class.

    // This can be useful for debugging XAML:
    DebugSettings.IsBindingTracingEnabled = true;
    DebugSettings.BindingFailed +=
        (sender, args) => Debug.WriteLine(args.Message);

}

There are still cases where you don't get a good stack trace, but this is often helpful.

Another approach is to break when an exception is thrown (via Debug, Windows, Exception Settings).

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

3 Comments

Thank you, it's very usefull ! But why debugger can't set break to expected line ?
Tell Visual Studio to break when (all or any specific) exception is thrown. The stack trace will provide the method, but not thw line.
Yes ! I've checked all Common Language Runtime Exceptions and not it's working ! Thank you.

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.