[英]WPF: where i can catch application crash event?
I put this code in my entry point
: 我把这段代码放在我的entry point
:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
throw new NotImplementedException();
}
private void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
throw new NotImplementedException();
}
}
And add this code into some button
click: 并将此代码添加到某个button
单击:
int num = 10;
int t = 5;
t = t - 5;
int error = num / t;
And my application crash but not go into this events. 我的应用程序崩溃但没有进入此事件。
Try adding this code in App.xaml.cs 尝试在App.xaml.cs中添加此代码
public App() : base() {
this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
MessageBox.Show("Unhandled exception occurred: \n" + e.Exception.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
If you run in debug mode then your app will break as soon as you hit an error. 如果您在调试模式下运行,那么一旦遇到错误,您的应用就会中断。 You would need to disable that. 你需要禁用它。 Press Ctrl + Alt + E to see the Exception settings window and uncheck some. 按Ctrl + Alt + E查看“异常”设置窗口并取消选中。 Remember to turn it back though. 记得把它转回来。
The full list of handlers: https://code.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b 完整的处理程序列表: https : //code.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b
You can simulate an error just by throwing one. 您可以通过抛出一个来模拟错误。 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creating-and-throwing-exceptions https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creating-and-throwing-exceptions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.