Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions debugging with async and UWP

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 ?

like image 779
IL_Agent Avatar asked May 20 '16 14:05

IL_Agent


People also ask

What is exception handling in debugging?

Exception handling is all about ensuring that when your program encounters an issue, it will continue to run and provide informative feedback to the end-user or program administrator.

How do I fix unhandled exception in Visual Studio?

In Visual Studio, when exceptions are thrown or end up unhandled, the debugger can help you debug these by breaking just like it breaks when a breakpoint is hit.


1 Answers

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).

like image 163
Petter Hesselberg Avatar answered Sep 23 '22 09:09

Petter Hesselberg