C#Winforms处理未处理的异常

时间:2019-05-26 12:15:50

标签: c# multithreading winforms exception

我想捕获应用程序中所有未处理的异常。所以我用下面的代码捕获了所有未处理的异常:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

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

    static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        SaveEx(e.Exception);
        Application.Exit();
    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        SaveEx((Exception)e.ExceptionObject);
        Application.Exit();
    }

    static void SaveEx(Exception ex)
    {
        bool exists = System.IO.Directory.Exists(Path.GetDirectoryName(@"C:\AppLogs\"));

        if (!exists)
            System.IO.Directory.CreateDirectory(Path.GetDirectoryName(@"C:\AppLogs\"));


        String filePath = @"C:\AppLogs\" + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".txt";

        String log = "===========Start=============\r\n";

        log += "Error Message: " + ex.Message + "\r\n";
        log += "Stack Trace: " + ex.StackTrace + "\r\n";

        log += "===========End=============";

        System.IO.File.WriteAllText(filePath, log);
    }
}

我试图用零除引发这些异常:

在主线程上工作完美:

int t = 0;
int r = 5 / t;

但是当我尝试在Thread内进行操作时:

 Thread thread = new Thread(delegate()
 {
      int t = 0;
      int r = 5 / t;
 });
 thread.Start();

调用了CurrentDomain_UnhandledException函数,但是它一直在我的代码中调用int r = 5 / t;行,所以我遇到了一个异常循环。知道可能是什么问题吗?该线程仅被调用一次。

1 个答案:

答案 0 :(得分:1)

您需要更改应用程序UnhandledExceptionMode  到UnhandledExceptionMode.CatchException以使事情正常运行。微软已经写了一篇很好的文章here

我试图模拟您的工作。一切正常,看看我的例子。

    [STAThread]
    static void Main()
    {
        // Set the unhandled exception mode to force all Windows Forms errors to go through
        // our handler.
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

        // Add the event handler for handling UI thread exceptions to the event.
        Application.ThreadException += Application_ThreadException;

        // Add the event handler for handling non-UI thread exceptions to the event. 
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

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

    private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        MessageBox.Show(e.Exception.Message, "Application_ThreadException");
        Application.Exit();
    }

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        var exception = e.ExceptionObject as Exception;
        MessageBox.Show(exception.Message, "CurrentDomain_UnhandledException");
        Application.Exit();
    }

因此,如果我们在如下所示的线程内引发异常:

    private void button1_Click(object sender, EventArgs e)
    {
        var thread = new Thread(delegate () {
            throw new DivideByZeroException();
        });

        thread.Start();
    }
  1. button1_Click被解雇了。
  2. 线程开始并引发一个DividedByZeroException
  3. CurrentDomain_UnhandledException捕获异常,显示消息并关闭应用程序