【问题标题】:Exceptions debugging with async and UWP使用异步和 UWP 调试异常
【发布时间】:2016-09-17 21:05:33
【问题描述】:

我正在开发 UWP 应用程序。而且我经常使用 async/await。总是当我的代码中发生异常时,调试器会在 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

但我想查看异常发生时的行。如何实现这种行为?

【问题讨论】:

  • 您必须处理抛出异常的地方。例如尝试捕获?不知道你在这里寻找什么答案
  • 可能是 XAML 异常。
  • @Default,问题是我不知道它在哪里抛出,我想让调试器为我显示它))
  • @Janak,不,例如 NullReferenceException,在视图模型中

标签: c# debugging async-await uwp unhandled-exception


【解决方案1】:

将以下方法添加到您的 App 类中:

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;
    }
}

在您的 App 构造函数中:

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

}

仍然存在无法获得良好堆栈跟踪的情况,但这通常很有帮助。

另一种方法是在抛出异常时中断(通过DebugWindows异常设置)。

【讨论】:

  • 谢谢,很有用!但是为什么调试器不能将中断设置为预期的行?
  • 告诉 Visual Studio 在抛出(所有或任何特定的)异常时中断。堆栈跟踪将提供方法,但不提供行。
  • 是的!我检查了所有公共语言运行时异常,但它没有工作!谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-11-18
  • 2014-06-03
  • 2014-01-08
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多