How to implement one “catch'em all” exception handler with resume?

前端 未结 9 609
梦如初夏
梦如初夏 2020-11-29 07:07

I wonder how can I write a catch\'em all exception handler in the application level which will give the user the option to resume the application f

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 07:44

    I assume you are writing a Windows application in which case, yes, you can do this. I will leave the rights and wrongs of whether or not you should to others. There are already enough answers which look at this and I suggest you consider them carefully before you actually do this.

    Note, that this code will behave differently in the debugger than it does if you run the application directly (another reason not to do it perhaps). To get the application to show the messagebox and to continue on thereafter you will need to run the application from explorer, not from visual studio.

    Create a new Windows forms application. The code in Program.cs looks something like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication2 {
        static class Program {
            /// 
            /// The main entry point for the application.
            /// 
            [STAThread]
            static void Main() {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Form1 form1 = new Form1();
                Application.ThreadException += new ThreadExceptionEventHandler(form1.UnhandledThreadExceptionHandler);
                Application.Run(form1);
            }
        }
    }
    

    Then make the code in Form1 look something like this:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication2 {
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
            }
    
            public void UnhandledThreadExceptionHandler(object sender, ThreadExceptionEventArgs e) {
                this.HandleUnhandledException(e.Exception);
            }
    
            public void HandleUnhandledException(Exception e) {
                // do what you want here.
                if (MessageBox.Show("An unexpected error has occurred. Continue?",
                    "My application", MessageBoxButtons.YesNo, MessageBoxIcon.Stop,
                    MessageBoxDefaultButton.Button2) == DialogResult.No) {
                    Application.Exit();
                }
            }
    
            private void button1_Click(object sender, EventArgs e) {
                throw new ApplicationException("Exception");
            }
    
        }
    }
    

    (Add button1 to the form and attach it button1_Click.)

提交回复
热议问题