When we write the code to the code error exception handling sometimes will not do deal
For example, we execute the following code will cause the program to crash
-
private void Button_Click(object sender, RoutedEventArgs e)
-
{
-
throw
new Exception(
"字符串引发异常 日志输出");
-
}
This is unacceptable to us
When we encounter a problem to find one to fear that there is no one way to intercept all exceptions that otherwise hang up program?
Some WPF in dispatcherUnhandledException
We rewrite Onstartup method in APP
-
public
partial
class
App :
Application
-
{
-
protected override void OnStartup(StartupEventArgs e)
-
{
-
base.OnStartup(e);
-
DispatcherUnhandledException += App_DispatcherUnhandledException;
-
}
-
-
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
-
{
-
MessageBox.Show(e.Exception.ToString());
-
e.Handled =
true;
-
// throw new NotImplementedException();
-
}
-
}
Here we can intercept all unhandled exceptions can write log or reboot