Only One Try...End Try for all all the procedures...

  • Thread starter Thread starter I_AM_DON_AND_YOU?
  • Start date Start date
I

I_AM_DON_AND_YOU?

Scenario:

I have 10 buttons on the Windows form. All the 10 buttons have lengthy code.
I don't want to put Try..End Try Exception Handler in all the 10 procedure
handling 10 clicks events of 10 buttons. Is it possible that I only use only
one exception handler for my complete FORM or PROJECT or
APPLICATION....................... I know this sounds like a stupid
idea.....but this is not the issue....the issue is whether it can be
done................
 
Not stupid at all - in fact very useful.

The basic strategy is:

1) Start your application with a sub main
2) In the sub main create a new instance of your "main" form (lets call it
yourform)
3) wrap application.run(yourform) in a try catch block.

Any errors that bubble to the top will be handled. However, it will also
cause your app to exit.

If you want a global error handler that doesn't necessarily exit, check out
this example on gotdotnet. I think this technique is great. I've used it to
help users send emails to me with stack traces of unanticpated behavior,
while allowing them to resume execution until they can save and restart or
what not.

http://samples.gotdotnet.com/quickstart/howto/doc/WinForms/WinFormsAppErrorH
andler.aspx
 
Justin Weinberg said:
Not stupid at all - in fact very useful.

I'm not saying its not stupid. Because I do it on my code so my code
doesn't exit. I think the global event handler (as used at gotdotnet) is a
much better approach...

However, I am a big proponent of using many Try Catches, that way you have
less GIGO... At least I do... My applications may be a bit bulkier than
others, but if something goes wrong, I know exactly where and why.

But I'm anal like that...

and weird... =)
 
Back
Top