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

后端 未结 4 542
我在风中等你
我在风中等你 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:33

    An easy fix is not to run under the debugger.

    The debugger is masking the exception for some reason. If you run your app normally (Ctrl+F5), you'll get the usual "Unhandled exception has occurred in your application... Continue/Quit?" dialog.

    0 讨论(0)
  • 2020-12-01 14:33

    Having experienced this often and identified the issue regarding 64 bit OS and the Form.Load event, I always just make a point of doing all my start up functions in the Form.Shown event. For all practical purposes this is the same thing (aside from a few rare, exceptional circumstances), and the JIT message is produced in the Shown event.

    0 讨论(0)
  • 2020-12-01 14:36

    In your Program.cs' Main function you should also ensure that you've wrapped your call to open the form in a try/catch. Additionally use the AppDomain.UnhandledException to catch exceptions. We also add Application.ThreadException too.

    I believe the following will give you hooks into all the exceptions that can be thrown...

    static void Main()
    {
        try
        {
            System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException);
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
    
            var form = new MainForm();
            form.ShowDialog();
        }
        catch (Exception e)
        {
            HandleUnhandledException(e);
        }
        finally
        {
            // Do stuff
        }
    }
    
    private static void HandleUnhandledException(Object o)
    {
        // TODO: Log it!
        Exception e = o as Exception;
    
        if (e != null)
        {
    
        }
    }
    
    private static void OnUnhandledException(Object sender, UnhandledExceptionEventArgs e)
    {
        HandleUnhandledException(e.ExceptionObject);
    }
    
    private static void OnGuiUnhandedException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        HandleUnhandledException(e.Exception);
    }
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题