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

Force App to Roll Over and Die

P: n/a
Hello,

I was recently asked how to ensure an app falls over when an exception
occurs that has not been caught.
The framework will often prompt the user to Quit or continue in such case
and offer a little Stack Dump as well.

I was asked how to prevent this and just let the app die.....I didn't know
the answer...
possibly because i have yet to write anything with a bug in it..... ;)

I am assuming it is some fairly trivial setting?

Thanks
Richard
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

Maybe try something like this. Start the application from sub main

Public Sub Main
Try
Application.Run(new Form1)
Catch ex as exception
' Log error here and let user know app is closing because of error
End try
End sub

Ken
--------------------
"Richard Myers" <fa**@address.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,

I was recently asked how to ensure an app falls over when an exception
occurs that has not been caught.
The framework will often prompt the user to Quit or continue in such case
and offer a little Stack Dump as well.

I was asked how to prevent this and just let the app die.....I didn't know
the answer...
possibly because i have yet to write anything with a bug in it..... ;)

I am assuming it is some fairly trivial setting?

Thanks
Richard

Nov 21 '05 #2

P: n/a
I'd maybe catch for an exception in global exception class and maybe when an
exception occurs loop through all the processes and kill the process.

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uj**************@tk2msftngp13.phx.gbl...
Hi,

Maybe try something like this. Start the application from sub main
Public Sub Main
Try
Application.Run(new Form1)
Catch ex as exception
' Log error here and let user know app is closing because of error
End try
End sub

Ken
--------------------
"Richard Myers" <fa**@address.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,

I was recently asked how to ensure an app falls over when an exception
occurs that has not been caught.
The framework will often prompt the user to Quit or continue in such case
and offer a little Stack Dump as well.

I was asked how to prevent this and just let the app die.....I didn't know
the answer...
possibly because i have yet to write anything with a bug in it..... ;)

I am assuming it is some fairly trivial setting?

Thanks
Richard

Nov 21 '05 #3

P: n/a
Richard,
The "easiest" way would be to use one or more Global Exception Handlers,
which after logging the error causes your application to exit...

Wrapping Application.Run with a Try Catch (as Ken showed) will only catch
exceptions in the constructor of your main form.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay
"Richard Myers" <fa**@address.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,

I was recently asked how to ensure an app falls over when an exception
occurs that has not been caught.
The framework will often prompt the user to Quit or continue in such case
and offer a little Stack Dump as well.

I was asked how to prevent this and just let the app die.....I didn't know
the answer...
possibly because i have yet to write anything with a bug in it..... ;)

I am assuming it is some fairly trivial setting?

Thanks
Richard

Nov 21 '05 #4

P: n/a
O.k Ken, David, & Jay thanks thats plenty of good info.

What you all essentially seem to be saying is that it is a design flaw to
ever let the exception go uncaught, and that
it should at least be caught at the application entry point.... we cant
turn this dialogue off... and we should instead
correct the design flaw that surrenders control to the framework when this
uncaught exception occurs?

Richard
Nov 21 '05 #5

P: n/a
Richard
What you all essentially seem to be saying is that it is a design flaw to
ever let the exception go uncaught, I would say the only "design flaw" is to not use Global Exception Handlers.

I also consider over using Try/Catch blocks to be a "design flaw" I only use
a Try/Catch when I have something specific to do with the exception, IMHO
logging the exception is not something specific as that can be handled by
the Global Exception Handlers. Nor is catching an exception simply to
rethrow it, as the normal Exception processing does this intrinsically.

A good use of Try/Catch is catching an UnauthorizedAccessException when
you're not authorized to a resource and you have a specific alternative
routine to use. Or the SQL Connection was closed due to network failure so I
need to create a new one...

I do, however, use a significant number of Try/Finally blocks to ensure
objects are properly disposed of. In VB.NET 2005 & current versions of C#
these Try/Finally blocks can be replaced with Using statements.
we cant
turn this dialogue off... Incorrect, you can turn it off by using one or more of the Global Exception
Handlers, when you use a Global Exception Handler the standard dialog is not
displayed. The only exception (no pun intended) are any exceptions that the
constructor of the MainForm may throw, which is where you need a Try/Catch
around Application.Run in your Main. I normally include a Try/Catch around
the entire Main subroutine.
and we should instead
correct the design flaw that surrenders control to the framework when this
uncaught exception occurs? No not quite... If you mean no Global Exception Handlers, then yes you need
to correct this design flaw & use Global Exception Handlers, if you mean not
enough Try/Catches then no using more Try/Catches would be a different kind
of design flaw...

Read the MSDN article I referenced in my other reply.

Hope this helps
Jay

"Richard Myers" <fa**@address.com> wrote in message
news:OI**************@tk2msftngp13.phx.gbl... O.k Ken, David, & Jay thanks thats plenty of good info.

What you all essentially seem to be saying is that it is a design flaw to
ever let the exception go uncaught, and that
it should at least be caught at the application entry point.... we cant
turn this dialogue off... and we should instead
correct the design flaw that surrenders control to the framework when this
uncaught exception occurs?

Richard

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.