By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

Stopping application crashing when there is an unhandled exception

P: n/a
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.

Jan 4 '07 #1
Share this Question
Share on Google+
20 Replies


P: n/a
<jo**************@hotmail.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
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...?
Jan 4 '07 #2

P: n/a

Mark Rae wrote:
<jo**************@hotmail.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
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.

Jan 4 '07 #3

P: n/a
<jo**************@hotmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
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.
http://www.developer.com/net/asp/article.php/961301
Jan 4 '07 #4

P: n/a
<jo**************@hotmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
>
Mark Rae wrote:
><jo**************@hotmail.comwrote in message
news:11*********************@s34g2000cwa.googlegr oups.com...
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.

Jan 4 '07 #5

P: n/a
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
Jan 4 '07 #6

P: n/a

Mark Rae wrote:
<jo**************@hotmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
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.

http://www.developer.com/net/asp/article.php/961301
Its a application, not web application

Jan 4 '07 #7

P: n/a
"Marc Gravell" <ma**********@gmail.comwrote in message
news:ud**************@TK2MSFTNGP04.phx.gbl...
>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)
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...
Jan 4 '07 #8

P: n/a
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_Unhan dledException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
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

*** Sent via Developersdex http://www.developersdex.com ***
Jan 4 '07 #9

P: n/a

Boban Stojanovski wrote:
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_Unhan dledException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
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

*** Sent via Developersdex http://www.developersdex.com ***
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

Jan 4 '07 #10

P: n/a
Hi,

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
<jo**************@hotmail.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
>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.
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.

--
Ignacio Machin
machin AT laceupsolutions com
Jan 4 '07 #11

P: n/a
Hi,

<jo**************@hotmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
>
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.

Jan 4 '07 #12

P: n/a
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.

Jan 4 '07 #13

P: n/a
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.

Jan 4 '07 #14

P: n/a
"Marc Gravell" <ma**********@gmail.comwrote in message
news:ud**************@TK2MSFTNGP04.phx.gbl...
>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
Right I see :-(

Willy.

Jan 4 '07 #15

P: n/a
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.

jo**************@hotmail.com wrote:
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.
Jan 4 '07 #16

P: n/a
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.

jo**************@hotmail.com wrote:
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.
Jan 4 '07 #17

P: n/a
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.
--
http://www.markdawson.org
http://themightycoder.spaces.live.com
"jo**************@hotmail.com" wrote:
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.

Jan 5 '07 #18

P: n/a
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/lib...65(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.

<jo**************@hotmail.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
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.

Jan 5 '07 #19

P: n/a
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/lib...65(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.

Jan 5 '07 #20

P: n/a
Glad it helped, but there's nothing more permanent then a temporary fix. I
personally believe that threading and error handling is so important that I
would stop all other work until those problems were resolved. The more code
that gets piled on top of buggy code the harder it is to fix.
<jo**************@hotmail.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
>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/lib...65(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.

Jan 5 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.