我有两个表单。我的第一个表单上有一个按钮,当点击按钮时应该加载第二个表单。第二个表单在其Load事件中抛出异常。显示第二个表单的代码在try-catch块中。然而,在点击按钮时,该异常未被捕获到try-catch块中,我会得到一个“未处理异常”窗口。以下是代码:
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");
}
}
为什么我的代码没有捕获到异常?