Catch an Exception thrown by another form

后端 未结 4 1506
无人及你
无人及你 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:07

    You'll be able to do this as follows:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this);
            form2.Show();
        }
    
        public void HandleForm2Exception(Exception ex)
        {
            MessageBox.Show("EXCEPTION HAPPENED!");
        }
    }
    

    and on Form2.cs

    public partial class Form2 : Form
    {
        private Form1 form1;
    
        public Form2(Form1 form1) : this()
        {
            this.form1 = form1;
        }
    
        public Form2()
        {
            InitializeComponent();
        }
    
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                throw new Exception();
            }
            catch (Exception ex)
            {
                if(this.form1 != null)
                    this.form1.HandleForm2Exception(ex);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-19 11:15

    I don't think this can work, the new form is not running in the context of your code above, it is only being launched by it. If you check the stacktrace for the exception thrown you shouldn't see the code above in it, so it can't catch the exception.

    Update: I just created a test project & tried it. The stacktrace knows nothing about the original form. If you want to catch unhandled exceptions you may want to check out this question Unhandled Exception Handler in .NET 1.1

    0 讨论(0)
  • 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");
                }
            }
    
    0 讨论(0)
  • 2021-01-19 11:17

    Why are you trying throw an exception from one form to another? "Don't throw new Exception()"

    If you are trying to let the main form know that the options form is closed you could just have a flag on the main form which is set from the options form.

    0 讨论(0)
提交回复
热议问题