3

I have two forms. My first form has a button on it, which should load the second form when the button is clicked. The second form throws an exception in its Load event. The code that shows the second form is in a try-catch block. However, the exception is not caught in that try-catch block, and I get an "unhandled exception" window when I click the button. Here's the code:

public partial class FirstForm : Form
{
   private void button_Click(object sender, EventArgs e)
   {
      try
      {
         // Showing this form will throw an exception
         SecondForm form = new SecondForm();
         form.Show();
      }
      catch (Exception ex)
      {
         // The exception is not caught here
         MessageBox.Show(ex.ToString());
      }
   } 
}

public partial class SecondForm : Form
{
   // Throw an exception when this form is shown
   private void form_Load(object sender, EventArgs e)
   {
      throw new Exception("something");
   }
}

Why isn't the exception caught in my code?

Ben Rubin
  • 6,909
  • 7
  • 35
  • 82
  • [Related post on MSDN forums](https://social.msdn.microsoft.com/Forums/vstudio/en-US/b8c82b79-47d4-42ab-abdb-ff71d67a0022/exception-thrown-in-formload-event-is-never-caught-by-ide?forum=vsdebug). The resolution isn't real clear but might help – M.Babcock Jun 13 '16 at 19:27
  • Look at [this](http://stackoverflow.com/questions/3209706/why-the-form-load-cant-catch-exception) SO question (last answer). – Jeroen Heier Jun 13 '16 at 19:29
  • I tried those solutions before posting my question. The "Prefer 32-bit" option was already unchecked in my build settings. I'm on Windows 10, and that answer said that this problem was fixed in Windows 8 and later, but I'm still having the problem. The answers that I've seen talk about the exception getting silently swallowed or only being a problem while the debugger is running, but that's not what I'm experiencing. My exceptions bubble up and cause the program to crash, so they're not getting swalled, but they're not allowing me to handle them either. – Ben Rubin Jun 13 '16 at 19:40
  • 1
    This is entirely by design, exceptions in *none* of the events of the Form class are catchable, the Load event is not special. You at best could get a notification from the Application.UnhandledException event but this is disabled intentionally when you debug. Luckily the Load event never requires you to do anything risky so you can easily move the code into the constructor instead. If you can't for some reason then you have to use try/catch in your event handler. – Hans Passant Jun 13 '16 at 21:21
  • Thanks for the explanation. If you want to add that as an answer, I'll mark it as the correct answer. – Ben Rubin Jun 13 '16 at 21:29

1 Answers1

0

You could use ShowDialog(this) istead of Show method when you show the second form, to make the first form take control of the second. Neither with ShowDialog(this) you will be able to cath the exception. I think this question already has an answer here

Community
  • 1
  • 1
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76