例外不可避免,所有地方都寫try...catch也麻煩,所以有了未處理例外的處理的東東,分別為以下三個事件:
- Application.Current.DispatcherUnhandledException:DispatcherUnhandledException is raised by an Application for each exception that is unhandled by code running on the main UI thread.(UI執行緒中任何未處理的例外將觸發該事件)
- System.Threading.Tasks.TaskScheduler.UnobservedTaskException:Occurs when a faulted task's unobserved exception is about to trigger exception escalation policy(出錯的任務中未觀察到的例外將觸發該事件),在Task被垃圾回收的時候,解構式檢測到該Task物件還有未被處理過的例外,會拋出這個例外并觸發,所以感覺總是慢一拍,
- AppDomain.CurrentDomain.UnhandledException:應用程式域的未處理例外
第1、2個事件很好理解,在UI執行緒和任務執行緒throw new Excepiton("測驗")都能測驗出來,第3個事件我是折騰好久才找到觸發的地方——比如在1、2事件的方法中又發生了例外,所以也可以理解1、2事件都有引數和方法可以設定成已處理(e.Handled=True、e.SetObserved()),第3個事件一被觸發,game over,
最后附上我的代碼
using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace L3_Exception
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}
/// <summary>
/// 非主執行緒錯誤
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (Exception item in e.Exception.InnerExceptions)
{
sb.AppendLine($@"例外型別:{item.GetType()}
例外內容:{item.Message}
來自:{item.Source}
{item.StackTrace}");
}
e.SetObserved();
Restart("Task Exception", sb.ToString());
}
/// <summary>
/// App里面的錯誤
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
StringBuilder sb = new StringBuilder();
try
{
Exception ex = e.ExceptionObject as Exception;
sb.AppendLine($@"例外型別:{ ex.GetType()}
例外內容:{ ex.Message}
內部例外內容:{ex?.InnerException?.Message}
來自:{ ex.Source}
{ ex.StackTrace}");
}
catch
{
sb.AppendLine("不可恢復的WPF表單執行緒例外");
}
Restart("Domain Exception", sb.ToString());
}
/// <summary>
/// 主執行緒錯誤
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($@"例外型別:{ e.Exception.GetType()}
例外內容:{ e.Exception.Message}
內部例外內容:{e.Exception?.InnerException?.Message}
來自:{ e.Exception.Source}
{ e.Exception.StackTrace}");
e.Handled = true;
Restart("主執行緒例外", sb.ToString());
}
private static void Restart(string title, string content)
{
MessageBox.Show(content, title);
//Current.Dispatcher.Invoke(() => Current.Shutdown(-1));
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/69618.html
標籤:其他
