wpf中捕获mysql连接异常_wpf 全局异常捕获处理

namespaceWpfGridChange

{usingSystem;usingSystem.CodeDom.Compiler;usingSystem.Diagnostics;usingSystem.Windows;using System.Threading.Tasks;public classApp : Application

{

[DebuggerNonUserCode, GeneratedCode("PresentationBuildTasks", "4.0.0.0")]public voidInitializeComponent()

{base.StartupUri = new Uri("../Resources/xaml/WinWellcome.xaml", UriKind.Relative);

}protected override voidOnStartup(StartupEventArgs e)

{

RegisterEvents();base.OnStartup(e);

}///

///注册事件///

private voidRegisterEvents()

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

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

this.DispatcherUnhandledException +=App_DispatcherUnhandledException;//非UI线程未捕获异常处理事件(例如自己创建的一个子线程)

AppDomain.CurrentDomain.UnhandledException +=CurrentDomain_UnhandledException;

}private static void TaskScheduler_UnobservedTaskException(objectsender, UnobservedTaskExceptionEventArgs e)

{try{var exception = e.Exception asException;if (exception != null)

{

HandleException(exception);

}

}catch(Exception ex)

{

HandleException(ex);

}finally{

e.SetObserved();

}

}//非UI线程未捕获异常处理事件(例如自己创建的一个子线程)

private static void CurrentDomain_UnhandledException(objectsender, UnhandledExceptionEventArgs e)

{try{var exception = e.ExceptionObject asException;if (exception != null)

{

HandleException(exception);

}

}catch(Exception ex)

{

HandleException(ex);

}finally{//ignore

}

}//UI线程未捕获异常处理事件(UI主线程)

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

{try{

HandleException(e.Exception);

}catch(Exception ex)

{

HandleException(ex);

}finally{//处理完后,我们需要将Handler=true表示已此异常已处理过

e.Handled = true;

}

}private static voidHandleException(Exception e)

{

MessageBox.Show("程序异常:" + e.Source + "\r\n@@" + Environment.NewLine + e.StackTrace + "\r\n##" + Environment.NewLine +e.Message);//记录日志Utils.LogWrite(e);

}

[GeneratedCode("PresentationBuildTasks", "4.0.0.0"), DebuggerNonUserCode, STAThread]public static voidMain()

{

App app= newApp();

app.InitializeComponent();

app.Run();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值