Console控制台程序
使用AppDomain.CurrentDomain.UnhandledException捕获所有线程的未处理异常
注1:执行完异常捕获的OnUncaughtExceptionHandler回调函数后,进程会立即退出。
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += OnUncaughtExceptionHandler; Test(); Console.ReadLine(); } private static void OnUncaughtExceptionHandler(object sender, System.UnhandledExceptionEventArgs args) { if (args != null && args.ExceptionObject != null) { Exception e = args.ExceptionObject as Exception; if (e != null) { Console.WriteLine("Message: {0}", e.Message); Console.WriteLine("StackTrace: {0}", e.StackTrace); } } } } }
捕获主线程的未处理异常
using System; namespace ConsoleApp1 { class Program { static void Test() {
int a = 12; int b = 0; int c = a / b; // 整数除零异常 System.DivideByZeroException } } }
捕获其他线程的未处理异常
using System; using System.Threading; namespace ConsoleApp1 { class Program { static void Test() { // 创建一个线程并启动执行 Thread TestThread = new Thread(() => { throw new Exception(); }); TestThread.Start(); } } }
WindowsForm窗体程序
使用AppDomain.CurrentDomain.UnhandledException捕获所有线程(UI线程和其他线程)的未处理异常
① 需要将Application的UnhandledExceptionMode设置为UnhandledExceptionMode.ThrowException(或UnhandledExceptionMode.Automatic)。
② 执行完异常捕获的OnUncaughtExceptionHandler回调函数后,进程会立即退出。
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); AppDomain.CurrentDomain.UnhandledException += OnUncaughtExceptionHandler; Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); Application.Run(new Form1()); } private static void OnUncaughtExceptionHandler(object sender, System.UnhandledExceptionEventArgs args) { if (args != null && args.ExceptionObject != null) { Exception e = args.ExceptionObject as Exception; if (e != null) { MessageBox.Show(string.Format("Message:{0}/nStackTrace:{1}", e.Message, e.StackTrace), "Uncaught Exception"); } } } } }
捕获UI线程的未处理异常
WindowsForm程序中UI线程即主线程,该线程中处理窗体中各种UI的消息。
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int a = 12; int b = 0; int c = a / b; // 整数除零异常 System.DivideByZeroException } } }
捕获其他线程的未处理异常
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) {
// 创建一个线程并启动执行 System.Threading.Thread TestThread = new System.Threading.Thread(() => { throw new Exception(); }); TestThread.Start(); } } }
使用Application.ThreadException捕获UI线程(主线程)的未处理异常
① Application.ThreadException仅能用于UI线程(主线程)的未处理异常捕获,无法捕获其他线程
② 需要将Application的UnhandledExceptionMode设置为UnhandledExceptionMode.CatchException。
③ UnhandledExceptionMode设置为UnhandledExceptionMode.CatchException后,即使绑定了AppDomain.CurrentDomain.UnhandledException全局捕获,UI线程(主线程)仍然只会被Application.ThreadException捕获
④ 执行完异常捕获的ApplicationThreadException回调函数后,进程不会退出,仍可继续运行。
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(ApplicationThreadException); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.Run(new Form1()); } static void ApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { MessageBox.Show(string.Format("Message:{0}/nStackTrace:{1}", e.Exception.Message, e.Exception.StackTrace), "Application Thread Exception"); } } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/282748.html