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?