App wide catchall exception handler

J

John

Hi

I have a vs2008 winform app that has a start-up form assigned through
application framework i.e. no Sub Main. How can I setup a catchall exception
handler to my app to catch any unexpected exceptions?

Thanks

Regards
 
K

kimiraikkonen

Hi

I have a vs2008 winform app that has a start-up form assigned through
application framework i.e. no Sub Main. How can I setup a catchall exception
handler to my app to catch any unexpected exceptions?

Thanks

Regards

Try-catch-finally?
 
P

Phill W.

John said:
I have a vs2008 winform app that has a start-up form assigned through
application framework i.e. no Sub Main.

Why not?
I haven't played with the Application Framework yet and, personally, I
/like/ Sub Main.
How can I setup a catchall exception handler to my app to catch
any unexpected exceptions?

Apart from the occasional left-field stuff that the Framework throws at
you, you shouldn't /have/ any unexpected exceptions.

Exceptions are exceptional, not "unexpected".

IMHO, this is one of the big weaknesses of .Net.
It hands you this big thing called "Exception Handling" on a platter,
then leaves /you/ to work out what to do with it. Other languages are
far more explicit and actually push you to keep Exception Handling very
much in mind as you write your code.

Every "entry point" into your code (Sub Main, methods that run on
Threads, any action that the user initiates) should have its /own/
Try..Catch constructs.

Having a global, "catch-all" Exception Handler is /limited/ in its use,
and then mainly as a last-resort, back-stop. The most you can do here
is to log the error to a file, somewhere. By the time you get here, the
program's teetering on the edge, ready to collapse in a messy heap.
There's nothing you can do to recover from (or /Handle/) the exception.
You have to do that /much/ closer to the site of the problem.

HTH,
Phill W.
 
J

John Saunders [MVP]

RobinS said:
While we all agree that our applications should not throw unhandled
exceptions, what if it does?

You tell us. If it does throw an unhandled exception, what will your
catch-all handler _do_ about it? Log the exception, and then, what?
 
D

Dave

John Saunders said:
You tell us. If it does throw an unhandled exception, what will your
catch-all handler _do_ about it? Log the exception, and then, what?

throw it, then restart... just like ms's failed initial attempt at sp1 for
vista, fail, restart, fail, restart, fail, restart, ad infinitum, ad
nauseum... sometimes its good to just give up.
 
J

John Saunders [MVP]

Dave said:
throw it, then restart... just like ms's failed initial attempt at sp1 for
vista, fail, restart, fail, restart, fail, restart, ad infinitum, ad
nauseum... sometimes its good to just give up.

If it's unhandled, then your application is probably not in a good enough
state for you to know that it makes sense to restart.

Depending on what kind of application it is, you're probably better off just
not catching such an exception.
 

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