Unhandled exceptions in runtime - is it a compiler bug?

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.
 
G

Guest

Viktar,
I think the main issue here is one of understanding the execution environment.

When code is executed in Visual Studio, this is a much different execution
environment than when code is deployed as a release build. As a release,
there is no IDE and no IDE debugger to attach and break into the editor.

There are some events you can catch and ways to wrap your code in unhandled
exception blocks that will aid in making sure you can "clean up" your
application. But the bottom line is that if your app throws an unhandled
exception in .NET 2.0, the appdomain unwinds and that's the end of it.
Peter

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
G

Guest

I am having this problem in both 1.1 and 2.0. But since they are saying this
is behavior by design, I doubt they going to change anything.

Actually, Microsoft support pointed me to that article.
 
W

Willy Denoyette [MVP]

message |I am having this problem in both 1.1 and 2.0. But since they are saying
this
| is behavior by design, I doubt they going to change anything.
|
| Actually, Microsoft support pointed me to that article.
|

Yep, I see what this is all about, and yes it's by design, I would love to
see an update of the article, so I posted a update request.

Willy.
 
D

David Levine

If it is by design then IMO the design needs to be changed. I never did like
the way exceptions in forms were handled.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top