Hi,
Sloan: I wondered about putting a general catch all handler in place,
but I tend to use lots of individual try/catch blocks around anything
that might throw an exception. I might try putting a try/catch around
Application.Run and individual try/catch around other blocks of code
where an exception can be handled better.
You really shouldn't catch exceptions that "might" be thrown unless you can
handle them gracefully in code.
Check out the following article for the recommended "local" exception handling
procedures:
"Exception Handling"
http://msdn2.microsoft.com/en-us/lib...05(VS.80).aspx
Adding an event handler to AppDomain.CurrentDomain.UnhandledException is a
good place to catch all instead of wrapping Application.Run in a try...catch.
You might rather use a pre-built library to handle exceptions on a "global"
level:
"Exception Handling Application Block"
http://msdn.microsoft.com/library/de.../html/ehab.asp
Dave: You are quite right, DivideByZero could be handled better, it was
just the first way of generating an exception that came in to my head -
Bad example on my part.
I guess Environment.Exit is better when dealing with fatal exceptions.
In most cases you won't be able to do much with true "fatal" exceptions, so
don't worry about those and just worry about the exceptions that you can
handle, even at the AppDomain level. For instance, what do you expect to do
when an OutOfMemoryException, ExecutionEngineException or
StackOverflowException is thrown?
If one of the aforementioned exceptions is thrown then your application might
just blow up but there's nothing you can really do in terms of "handling"
those exceptions so neither Application.Exit or Environment.Exit should need
to be called. In most other cases you will probably be able to at least log
the exception and try to continue running the application, but it's doubtful
that you'll be able to determine whether there will be any loss of state from
within a "global" exception handler. Therefore, calling Application.Exit will
at least allow your Forms one last change to gracefully shutdown if you must
shutdown the application (but I'd question your reasoning for doing so in the
first place).
I don't think you should worry about trying to end the application in code.
Instead, worry about how to safely keep it running.
--
Dave Sexton
"Bry" <br*********@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
Thanks for the replies.
Sloan: I wondered about putting a general catch all handler in place,
but I tend to use lots of individual try/catch blocks around anything
that might throw an exception. I might try putting a try/catch around
Application.Run and individual try/catch around other blocks of code
where an exception can be handled better.
Dave: You are quite right, DivideByZero could be handled better, it was
just the first way of generating an exception that came in to my head -
Bad example on my part.
I guess Environment.Exit is better when dealing with fatal exceptions.