J
Jos Beelen
Examine the following code fragment used from within the
main form of a windows forms application when clicking a
button:
private void button1_Click(object s, System.EventArgs e)
{
try
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show ("Error catched");
}
}
Form2 also contains a button and when the button is
clicked, a divide by zero exception is created by the
following code:
private void button2_Click(object s, System.EventArgs e)
{
int i,j;
i = 0;
j = 1/i; // Divide by zero
}
Because the exception is not catched in Form2, I expect
this exception is catched by the code fragment in the main
form by displaying the message "Error catched", but the
behaviour is very strange.
1. When running the application from within VS2003 using
F5 (=debug), the error is catched, so my messagebox is
displayed. This is the same for both running in release
mode and debug mode. This is the expected behaviour !!!!
2. When running the application from wihtin VS2003 using
ctrl-F5 (=no debug), the standard "unhandled exception"
window is displayed. This is the same for both running in
release mode and debug mode. This behaviour is also the
same when running the application outside the studio (=
the behaviour when the application is deployed).
Question:
Why is the exception in -2- not catched by the implemented
catch. I expected the same behaviour as in -1-
I know I can use Application.ThreadException to catch the
unhandled exception, but this is not the requested
functionality. I want the calling method to handle the
exception.
Who can help me ....
Thanks in advance.
main form of a windows forms application when clicking a
button:
private void button1_Click(object s, System.EventArgs e)
{
try
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show ("Error catched");
}
}
Form2 also contains a button and when the button is
clicked, a divide by zero exception is created by the
following code:
private void button2_Click(object s, System.EventArgs e)
{
int i,j;
i = 0;
j = 1/i; // Divide by zero
}
Because the exception is not catched in Form2, I expect
this exception is catched by the code fragment in the main
form by displaying the message "Error catched", but the
behaviour is very strange.
1. When running the application from within VS2003 using
F5 (=debug), the error is catched, so my messagebox is
displayed. This is the same for both running in release
mode and debug mode. This is the expected behaviour !!!!
2. When running the application from wihtin VS2003 using
ctrl-F5 (=no debug), the standard "unhandled exception"
window is displayed. This is the same for both running in
release mode and debug mode. This behaviour is also the
same when running the application outside the studio (=
the behaviour when the application is deployed).
Question:
Why is the exception in -2- not catched by the implemented
catch. I expected the same behaviour as in -1-
I know I can use Application.ThreadException to catch the
unhandled exception, but this is not the requested
functionality. I want the calling method to handle the
exception.
Who can help me ....
Thanks in advance.