如何捕获未处理的异常?如果我的所有代码都包含在 try catch 中? 但是还是发生了异常,应用程序崩溃了……也许有一些一般性的建议?
使用:
try
{
...my code
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"Error!",MessageBoxButton.OK);
}
示例代码不存在,因为有一个大代码并做了很多事情......看起来某处发生了错误,但 try-catch 没有显示,应用程序只是关闭......
添加:
var errorw = MessageBox.Show(e.ExceptionObject.ToString(), "error", MessageBoxButton.OK); e.Handled = true;
消息:参数不正确。
正如我们现在所理解的哪里和什么参数给出的是不正确的?顺便忘了写,当您返回到应用程序的上一页时,当您按返回时会发生错误。
请您参考如下方法:
你会这样试试吗
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender,ApplicationUnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is QuitException)
return;
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
//MessageBox.Show(e.ExceptionObject.ToString(), "Unexpected error", MessageBoxButton.OK);
var errorWin = new ErrorWindow(e.ExceptionObject, "An unexpected error has occurred. Please click Send to report the error details.")
{Title = "Unexpected Error"};
errorWin.Show();
//((PhoneApplicationFrame) RootVisual).Source = new Uri("/Views/ErrorWindow.xaml", UriKind.Relative);
e.Handled = true;
}
private class QuitException : Exception { }
public static void Quit()
{
throw new QuitException();
}
