Stopping application crashing when there is an unhandled exception

  • Thread starter joseph_gallagher
  • Start date
J

joseph_gallagher

Hi,

I've recently ported a .Net 1.1 application to .Net 2.0 and the one new
feature that is getting on my nerves is that when there is an unhandled
exception the application completely crashes, causing user to loose all
the work they have done where they could previously just click continue
and in 99% of cases be fine. Is there an option to turn this annoying
crash and loose everything option off, I can see it has its merits but
in my case its just annoying.

Joe.
 
M

Mark Rae

I've recently ported a .Net 1.1 application to .Net 2.0 and the one new
feature that is getting on my nerves is that when there is an unhandled
exception the application completely crashes, causing user to loose all
the work they have done where they could previously just click continue
and in 99% of cases be fine. Is there an option to turn this annoying
crash and loose everything option off, I can see it has its merits but
in my case its just annoying.

Well, the first question I guess is why might there be an unhandled
exception...? Are you not using exception handling i.e.
Try...Catch[...Finally]?

Secondly, why does your Global.asax not have an Application_Error
routine...?
 
J

joseph_gallagher

Mark said:
I've recently ported a .Net 1.1 application to .Net 2.0 and the one new
feature that is getting on my nerves is that when there is an unhandled
exception the application completely crashes, causing user to loose all
the work they have done where they could previously just click continue
and in 99% of cases be fine. Is there an option to turn this annoying
crash and loose everything option off, I can see it has its merits but
in my case its just annoying.

Well, the first question I guess is why might there be an unhandled
exception...? Are you not using exception handling i.e.
Try...Catch[...Finally]?

Secondly, why does your Global.asax not have an Application_Error
routine...?

The main cause of exceptions has been cross thread exceptions on
controls, which I know can be set to be allowed but I do want to root
them all out. Also as a general preference if a new bug finds its way
in I'd prefer the user to at least have the option to try to continue.

It handles the "AppDomain.CurrentDomain.UnhandledException" to log the
problem if thats what you mean, but after this the application still
exits.

All I'm really asking is there a way to avoid the application closing
on an unhandled exception, not how to avoid unhandled exceptions.
 
W

Willy Denoyette [MVP]

Mark said:
I've recently ported a .Net 1.1 application to .Net 2.0 and the one new
feature that is getting on my nerves is that when there is an unhandled
exception the application completely crashes, causing user to loose all
the work they have done where they could previously just click continue
and in 99% of cases be fine. Is there an option to turn this annoying
crash and loose everything option off, I can see it has its merits but
in my case its just annoying.

Well, the first question I guess is why might there be an unhandled
exception...? Are you not using exception handling i.e.
Try...Catch[...Finally]?

Secondly, why does your Global.asax not have an Application_Error
routine...?

The main cause of exceptions has been cross thread exceptions on
controls, which I know can be set to be allowed but I do want to root
them all out. Also as a general preference if a new bug finds its way
in I'd prefer the user to at least have the option to try to continue.

It handles the "AppDomain.CurrentDomain.UnhandledException" to log the
problem if thats what you mean, but after this the application still
exits.

All I'm really asking is there a way to avoid the application closing
on an unhandled exception, not how to avoid unhandled exceptions.


What do you mean with " cross thread exceptions on controls"? Web controls don't have thread
affinity, why should you ever get this kind of exceptions? Am I missing something?

Willy.
 
M

Marc Gravell

What do you mean with " cross thread exceptions on controls"? Web
controls don't have thread affinity, why should you ever get this
kind of exceptions? Am I missing something?

Unless I blinked, the OP never mentioned web; Mark did (via
Global.asax)

Marc
 
M

Mark Rae

Unless I blinked, the OP never mentioned web; Mark did (via Global.asax)

Apologies for causing any confusion - I quoted the link as an example of
global exception handing - probably should have looked for a WinForms one,
really...
 
B

Boban Stojanovski

You should handle unhandled exceptions :).

Code in your Program class will be like this:
//Main method:

Application.ThreadException += new
ThreadExceptionEventHandler(OnThreadException);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new formMDI());

//event handlers
private static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
MessageBox.Show("CurrentDomain_UnhandledException: " +
e.ExceptionObject);
}

private static void OnThreadException(object sender,
ThreadExceptionEventArgs t)
{
MessageBox.Show("OnThreadException: " + t.Exception);
}

Best Regards,
Boban
 
J

joseph_gallagher

Boban said:
You should handle unhandled exceptions :).

Code in your Program class will be like this:
//Main method:

Application.ThreadException += new
ThreadExceptionEventHandler(OnThreadException);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new formMDI());

//event handlers
private static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
MessageBox.Show("CurrentDomain_UnhandledException: " +
e.ExceptionObject);
}

private static void OnThreadException(object sender,
ThreadExceptionEventArgs t)
{
MessageBox.Show("OnThreadException: " + t.Exception);
}

Best Regards,
Boban

I have similar code in my app, but after the UnhandledException is
handled, the application still exits, what I need is to be able to
continue running the program like you can in .Net 1.1
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Mark Rae said:
I've recently ported a .Net 1.1 application to .Net 2.0 and the one new
feature that is getting on my nerves is that when there is an unhandled
exception the application completely crashes, causing user to loose all
the work they have done where they could previously just click continue
and in 99% of cases be fine.
[/QUOTE]

Well, you have to start handling your errors :) , if you know an operation
may fail wrap it in try/catch

The application will ends when it find an error that was not handled. I
don't see nothing wrong with that.

It's your responsability to either save the work and/or correctly handle the
errors.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

The main cause of exceptions has been cross thread exceptions on
controls, which I know can be set to be allowed but I do want to root
them all out.

You should then correctly handle that case, you should not try to update the
UI from a worker thread, if you do you may get problems (even if not an
exception) these are the kind of problems that are hard to find/correct.
 
J

joseph_gallagher

I'm not asking for advice on how to handle errors, I'm asking if it is
possible to stop .Net 2.0 from exiting the application in exactly the
same way .Net 1.1 does when there is an unhandled exception..

Please dont answer with code suggestions to prevent unhandled
exceptions occuring, its a simple question.
 
J

joseph_gallagher

The application will ends when it find an error that was not handled. I
don't see nothing wrong with that.

I see no problem with this been the default, but to make it the only
possible option, especially when it was not the case in previous
versions is annoying and not very backward compatible. I was kinda
hoping there would be a flag like the one for CrossThreadOperations on
controls or a way to stop the application closing in the
OnUnhandledException event.
 
A

Andy

Well, my exception handler doesn't close the application, so you must
be doing something wrong. Are you sure you're attaching a listener to
the System.Windows.Forms.Application.ThreadException event?

A better way to handle this though would be to fix the cause of the
exceptions.
 
A

Andy

Well, my exception handler doesn't close the application, so you must
be doing something wrong. Are you sure you're attaching a listener to
the System.Windows.Forms.Application.ThreadException event?

A better way to handle this though would be to fix the cause of the
exceptions.

One final note, there are some exceptions like OutOfMemoryException
that cannot be caught.
 
G

Guest

I see no problem with this been the default, but to make it the only
possible option, especially when it was not the case in previous
versions is annoying and not very backward compatible.

The only difference now is that before your apps were working incorrectly
and you didn't realize it, now your app works incorrectly and you do realize.
I would say that trying to be backwards compatible would be the wrong thing
to do, better to let the user know something has gone wrong and lose data
upto a certain point rather than let them continue and possibly lose even
more work.

Mark.
 
D

David Levine

You can force the 2.0 runtime to use the same default behavior for unhandled
exceptions as the 1.1 runtime. There's a compatibility flag called
legacyUnhandledExceptionPolicy that you can add to the app.config file,
as...
<legacyUnhandledExceptionPolicy enabled="1"/>This link explains it:
http://msdn2.microsoft.com/en-us/library/ms228965(VS.80).aspx

I suggest only using this flag when using 3rd party libraries that you
cannot get fixed: there's never a good reason to use it for your own code.
 
J

joseph_gallagher

You can force the 2.0 runtime to use the same default behavior for unhandled
exceptions as the 1.1 runtime. There's a compatibility flag called
legacyUnhandledExceptionPolicy that you can add to the app.config file,
as...
<legacyUnhandledExceptionPolicy enabled="1"/>This link explains it:
http://msdn2.microsoft.com/en-us/library/ms228965(VS.80).aspx

I suggest only using this flag when using 3rd party libraries that you
cannot get fixed: there's never a good reason to use it for your own code.

Thanks, thats exactly what I'm after.

I can see the point about not using this flag out of principle, but at
present I have an app that has a couple of months of new development in
2.0 and will take another couple of months of make bug free in 2.0,
faced with a choice between rolling back 2 months work, repeating and
spending 2 months fixing bugs before getting onto 2.0 and using a flag
to allow me to continue using the newer version while fixing the bugs,
I'm happy using the flag.
 

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