G
Guest
Hi everybody,
let me start with a simple sample to replicate the problem:
1. Create a new Windows Application C# project with 2 forms - MainForm and
FaultyForm;
2. In the faulty form override the OnLoad method to throw an exception:
public partial class FaultyForm : Form
...
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
throw new ApplicationException("Oops......");
}
3. Add a button to the MainForm and on button click event try to ShowDialog
of a FaultyForm, of course in a try...catch block:
public partial class MainForm : Form
...
private void clickMeButton_Click(object sender, EventArgs e)
{
try
{
FaultyForm f = new FaultyForm();
f.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Handled Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
==============================================
Now, if you will run this sample inside your VS in debug mode, everything is
fine. The exception will be handled and you will get a nice error message.
The interesting part is that if you will do "Start without debugging" or just
try to execute an .exe file, the exception will not be handled and
applications will crash !!!
Now imagine my frustration when I was getting complains that my applications
do crash in production while I was very carefully debugging them and could
not replicate the problem
While I understand that I can and probably should handle exception inside
every method of every form, I do believe that this is a bug in VS.
If anybody can suggest how I can ensure that my application that is
currently in production (a pretty big project) will not carsh without
revisiting every method in the child forms and wiring events to propogate
child form exceptions to the main form without crashing an application, I
would highly appretiate !
Also, would be nice to know if Microsoft is aware of this problem and what
do they say, as I didn't find out any information about that.
let me start with a simple sample to replicate the problem:
1. Create a new Windows Application C# project with 2 forms - MainForm and
FaultyForm;
2. In the faulty form override the OnLoad method to throw an exception:
public partial class FaultyForm : Form
...
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
throw new ApplicationException("Oops......");
}
3. Add a button to the MainForm and on button click event try to ShowDialog
of a FaultyForm, of course in a try...catch block:
public partial class MainForm : Form
...
private void clickMeButton_Click(object sender, EventArgs e)
{
try
{
FaultyForm f = new FaultyForm();
f.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Handled Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
==============================================
Now, if you will run this sample inside your VS in debug mode, everything is
fine. The exception will be handled and you will get a nice error message.
The interesting part is that if you will do "Start without debugging" or just
try to execute an .exe file, the exception will not be handled and
applications will crash !!!
Now imagine my frustration when I was getting complains that my applications
do crash in production while I was very carefully debugging them and could
not replicate the problem
While I understand that I can and probably should handle exception inside
every method of every form, I do believe that this is a bug in VS.
If anybody can suggest how I can ensure that my application that is
currently in production (a pretty big project) will not carsh without
revisiting every method in the child forms and wiring events to propogate
child form exceptions to the main form without crashing an application, I
would highly appretiate !
Also, would be nice to know if Microsoft is aware of this problem and what
do they say, as I didn't find out any information about that.