捕获未处理的异常

 处理程序中没有被try-catch捕获的异常,避免程序异常退出(或者在退出之前记录日志信息)。

using System;
using System.Threading;
using System.Windows.Forms;

namespace CatchUnhandledException
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Form1.UIThreadExceptionHander);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);// 这一步需要在实例化主窗口之前
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);// 此举并不能阻止应用程序退出


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        /// <summary>
        /// 处理非UI线程引发的未处理异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = e.ExceptionObject as Exception;
                MessageBox.Show($"{ex.Message}\r\n详情:\r\n{ex.StackTrace}", "非UI线程引发的未处理异常");
            }
            catch (Exception exc)
            {
                try
                {
                    MessageBox.Show($"在处理非UI线程引发的未处理异常时出现致命错误:{exc.Message}");
                }
                finally
                {
                    Application.Exit();
                }
            }
        }
    }
}
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CatchUnhandledException
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 捕获未处理的UI线程异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void UIThreadExceptionHander(object sender, ThreadExceptionEventArgs t)
        {
            try
            {
                Exception ex = t.Exception;
                MessageBox.Show($"{ex.Message}\r\n详情:\r\n{ex.StackTrace}", "UI线程中的未处理异常");
            }
            catch (Exception exc)
            {
                try
                {
                    MessageBox.Show($"在处理UI线程中的未处理异常时出现致命错误:{exc.Message}\r\n详情:\r\n{exc.StackTrace}");
                }
                finally
                {
                    Application.Exit();
                }
            }
        }

        private void btnUIException_Click(object sender, EventArgs e)
        {
            throw new Exception("UI线程异常");
        }

        private void btnNonUIException_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(() => { throw new Exception("非UI线程异常"); });
            thread.Start();
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值