/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
//private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
//{
// MessageBox.Show(e.Exception.Message);
//}
private void Application_Startup(object sender, StartupEventArgs e)
{
App.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show((e.ExceptionObject as Exception).Message);
}
void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message);
e.Handled = true;
//throw new NotImplementedException();
}
}
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
//private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
//{
// MessageBox.Show(e.Exception.Message);
//}
private void Application_Startup(object sender, StartupEventArgs e)
{
App.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show((e.ExceptionObject as Exception).Message);
}
void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message);
e.Handled = true;
//throw new NotImplementedException();
}
}
本文详细阐述了App启动过程中的关键逻辑,包括初始化、异常捕获与处理机制。通过设置DispatcherUnhandledException事件处理器和AppDomain的UnhandledException事件处理器,确保了应用在遇到未处理异常时能够优雅地进行错误展示和终止。

6118

被折叠的 条评论
为什么被折叠?



