简体   繁体   English

如何知道未处理的异常发生在哪里?

[英]How to know where the unhandled exception occured?

When I run the app, I keep getting this error: 当我运行该应用程序时,我不断收到此错误:

System.ArgumentException: Value does not fall within the expected range. System.ArgumentException:值不在预期范围内。

I don't know where this error occurs exactly or what is the cause. 我不知道此错误确切发生在哪里或是什么原因。 Isn't it possible to know that from: 是否有可能通过以下方式知道这一点:

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
    if (Debugger.IsAttached)
    {
        // An unhandled exception has occurred; break into the debugger
        Debugger.Break();
    }
}

in App.xaml ? App.xaml

How can I know where the unhandled exception occurs? 我怎么知道未处理的异常发生在哪里? thanks 谢谢

The following code will print the message and stack trace. 以下代码将打印消息和堆栈跟踪。 You can get where the exception occured from the stackTrace. 您可以从stackTrace中获取发生异常的位置。

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
    if (Debugger.IsAttached)
    {
        MessageBox.Show(e.ExceptionObject.Message + "\r\n" + e.ExceptionObject.StackTrace, "Error", MessageBoxButton.OK);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM