wpf中捕获mysql连接异常_WPF 捕捉全局异常

publicApp()

{//首先注册开始和退出事件

this.Startup += newStartupEventHandler(App_Startup);this.Exit += newExitEventHandler(App_Exit);

}void App_Startup(objectsender, StartupEventArgs e)

{//UI线程未捕获异常处理事件

this.DispatcherUnhandledException += newDispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);//Task线程内未捕获异常处理事件

TaskScheduler.UnobservedTaskException +=TaskScheduler_UnobservedTaskException;//非UI线程未捕获异常处理事件

AppDomain.CurrentDomain.UnhandledException += newUnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

}void App_Exit(objectsender, ExitEventArgs e)

{//程序退出时需要处理的业务

}void App_DispatcherUnhandledException(objectsender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)

{try{

e.Handled= true; //把 Handled 属性设为true,表示此异常已处理,程序可以继续运行,不会强制退出

MessageBox.Show("UI线程异常:" +e.Exception.Message);

}catch(Exception ex)

{//此时程序出现严重异常,将强制结束退出

MessageBox.Show("UI线程发生致命错误!");

}

}void CurrentDomain_UnhandledException(objectsender, UnhandledExceptionEventArgs e)

{

StringBuilder sbEx= newStringBuilder();if(e.IsTerminating)

{

sbEx.Append("非UI线程发生致命错误");

}

sbEx.Append("非UI线程异常:");if (e.ExceptionObject isException)

{

sbEx.Append(((Exception)e.ExceptionObject).Message);

}else{

sbEx.Append(e.ExceptionObject);

}

MessageBox.Show(sbEx.ToString());

}void TaskScheduler_UnobservedTaskException(objectsender, UnobservedTaskExceptionEventArgs e)

{//task线程内未处理捕获

MessageBox.Show("Task线程异常:" +e.Exception.Message);

e .SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值