How can I get WinForms to stop silently ignoring unhandled exceptions?

后端 未结 4 549
我在风中等你
我在风中等你 2020-12-01 13:57

This is getting extremely irritating. Right now I have a winforms application, and things were not working right, but no exceptions were being thrown as far as I could tell

4条回答
  •  暖寄归人
    2020-12-01 14:36

    Try the following.

    • Handle exceptions in your main application entry point.
    • Also, manage unhandled thread exceptions using a ThreadExceptionEventHandler

    This is the code snippet:

    [STAThread]
    public static void Main(string[] args)
    {
        try
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            //your program entry point
        }
        catch (Exception ex)
        {
           //manage also these exceptions
        }
    }
    
    private void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        ProcessException(e.Exception);
    }
    

提交回复
热议问题