P
peter z
I have a very simple C# windows app containing two forms. Form1 calls
Form2.ShowDialog() in a try/catch block. Form2 has a button on it that when
pressed throws an exception.
While running in the IDE debugger, the Form1 try/catch works as expected
(ie. the Form1 message box displays the exception message).
When running outside of the IDE (ie. double click on the EXE from Windows
Explorer), the try/catch is not behaving as expected but instead the .Net
runtime unhandled exception dialog pops up.
I'm sure it has to do with the fact that the exception gets thrown from
another form than the try/catch is in (therefore a different WndProc) but
can't figure out why the debugger handles it and the runtime does not. Does
anyone have any explanation for this?
I'd like to use this pattern if possible but not the way it's behaving right
now, that is unacceptable. Any ideas welcome. Even if it's to suggest that
this pattern is inherently wrong
-Peter
Here is the relevant code from each form:
Form1:
private void button1_Click(object sender, System.EventArgs e)
{
try
{
Form2 dlg = new Form2();
dlg.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Form2:
private void button1_Click(object sender, System.EventArgs e)
{
Throw();
}
private void Throw()
{
throw new Exception("Form2 exception");
}
Form2.ShowDialog() in a try/catch block. Form2 has a button on it that when
pressed throws an exception.
While running in the IDE debugger, the Form1 try/catch works as expected
(ie. the Form1 message box displays the exception message).
When running outside of the IDE (ie. double click on the EXE from Windows
Explorer), the try/catch is not behaving as expected but instead the .Net
runtime unhandled exception dialog pops up.
I'm sure it has to do with the fact that the exception gets thrown from
another form than the try/catch is in (therefore a different WndProc) but
can't figure out why the debugger handles it and the runtime does not. Does
anyone have any explanation for this?
I'd like to use this pattern if possible but not the way it's behaving right
now, that is unacceptable. Any ideas welcome. Even if it's to suggest that
this pattern is inherently wrong
-Peter
Here is the relevant code from each form:
Form1:
private void button1_Click(object sender, System.EventArgs e)
{
try
{
Form2 dlg = new Form2();
dlg.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Form2:
private void button1_Click(object sender, System.EventArgs e)
{
Throw();
}
private void Throw()
{
throw new Exception("Form2 exception");
}