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 ?

IL_Agent
  • 707
  • 3
  • 16
  • 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 – default May 20 '16 at 14:33
  • May be that is XAML exception. – Janak May 20 '16 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 )) – IL_Agent May 20 '16 at 15:30
  • @Janak, no, NullReferenceException for examle, in a view model – IL_Agent May 20 '16 at 15:30
  • Possible duplicate of [Visual Studio: How to break on handled exceptions?](http://stackoverflow.com/questions/116896/visual-studio-how-to-break-on-handled-exceptions) – default May 20 '16 at 16:04
  • or [this](http://stackoverflow.com/questions/1698154/force-break-on-any-exception-thrown-in-program) or [this](http://stackoverflow.com/questions/11183099/how-to-make-visual-studio-break-only-on-unhandled-exceptions) or [this](http://stackoverflow.com/questions/5597421/breakpoint-on-an-exception-in-visual-studio) – default May 20 '16 at 16:06

1 Answers1

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

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42