WinForms Global Exception Handling?

后端 未结 7 1284
甜味超标
甜味超标 2020-11-27 05:17

I have implemented a software which have a DLL library which contains a huge set of classes which includes all the methods for my software.

Now i want to be able to

7条回答
  •  独厮守ぢ
    2020-11-27 05:31

    Since its a winforms app you could just enclose Application.Run(new MainForm()); in a try catch block.

    static void Main()
    {
    try {
     Application.Run(new MainForm());
    } catch(SystemException)//just as an example 
    {
     //log or handle the error here. 
    }
    }
    

    I don't know any implications this kind of solution would cause, but I just told you what you needed.

    Other options are subscribing to Application.ThreadException event.

    Read more here: unhandledexceptions

    There is also AppDomain.UnhandledException and you should read the difference between them here on MSDN.

    From MSDN :

    For certain application models, the UnhandledException event can be preempted by other events if the unhandled exception occurs in the main application thread.

    In applications that use Windows Forms, unhandled exceptions in the main application thread cause the Application.ThreadException event to be raised. If this event is handled, the default behavior is that the unhandled exception does not terminate the application, although the application is left in an unknown state. In that case, the UnhandledException event is not raised. This behavior can be changed by using the application configuration file, or by using the Application.SetUnhandledExceptionMode method to change the mode to UnhandledExceptionMode.ThrowException before the ThreadException event handler is hooked up. This applies only to the main application thread. The UnhandledException event is raised for unhandled exceptions thrown in other threads.

提交回复
热议问题