Catch an Exception thrown by another form

后端 未结 4 1596
无人及你
无人及你 2021-01-19 10:48

I\'m trying to do this:

I\'m creating another form, which in it\'s FormClosed method throws an exception, that should be caught by the main form.

Main Form:<

4条回答
  •  青春惊慌失措
    2021-01-19 11:16

    you can handle all exception in your project from program.cs

    static class Program
            {
    
                [STAThread]
                static void Main()
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;
    
                    Application.ThreadException += Application_ThreadException;
                    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);         
                    Application.Run(new MainMDI());              
                }
                static void Application_ThreadException(Object sender, ThreadExceptionEventArgs e)
                {
                    MessageBox.Show(e.Exception.Message, "Application.ThreadException");
                }
    
                static void AppDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
                {
                    MessageBox.Show(((Exception)e.ExceptionObject).Message, "AppDomain.UnhandledException");
                }
            }
    

提交回复
热议问题