[英]how to catch an unhandled exception in windows phone 7 application?
how to catch an unhandled exception? 如何捕获未处理的异常? if all my code wrapped in a try catch
? 如果我的所有代码都包含在try catch
? but an exception occurs all the same, and the application crashes ... Maybe there is some general advice? 但是一个异常发生了,应用程序崩溃了...也许有一些一般的建议?
use: 采用:
try
{
...my code
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"Error!",MessageBoxButton.OK);
}
Sample code is not present, because there is a big code and done many things ... and it looks like somewhere an error occurs, but where it is try-catch does not show, and the application just closes ... 示例代码不存在,因为有一个很大的代码,并做了很多事情......它看起来像某个地方出现错误,但它是try-catch没有显示的地方,应用程序只是关闭...
add: 加:
var errorw = MessageBox.Show(e.ExceptionObject.ToString(), "error", MessageBoxButton.OK); e.Handled = true;
And the message: The parameter is incorrect. 并且消息:参数不正确。
Just as we now understand where and what parameter has given is not true? 正如我们现在明白给出的参数在哪里和哪个不正确? By the way forgot to write that the error occurs when you press Back, when you return to the previous page of the application. 顺便说一句,当你返回应用程序的上一页时,忘记写下当你按Back时发生错误。
would you try like this 你会这样试试吗
// 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();
}
There is always a manual. 总有一本手册。
UnhandledException event description. UnhandledException事件描述。
Have you tried the StackTrace
property of Exception
. 您是否尝试过Exception
的StackTrace
属性? It shows you where the exception has been thrown. 它显示了抛出异常的位置。
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace,"Error!",MessageBoxButton.OK);
}
Make sure that you build application in debug, not release mode. 确保在调试中构建应用程序,而不是在发布模式下。 Than, go to VS Debug-Exceptions menu and check all 'Thrown' column to enabled. 然后,转到VS Debug-Exceptions菜单并检查所有'Thrown'列是否已启用。 After that, launch application with attached debugger. 之后,使用附加的调试器启动应用程序。 Also, you can execute your code step-by-step. 此外,您可以逐步执行代码。
Put your code in Try-Catch Block. 将您的代码放入Try-Catch Block。 I was also facing such problem, but then handled by Exception Handling Method. 我也遇到了这样的问题,但后来由异常处理方法处理。
try
{
// your code
}
catch (Exception ex)
{
throw (ex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.