Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

A global catch-all for exceptions in Windows Forms applications

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
thechaosengine - 05 Aug 2005 20:19 GMT
Hi everyone,

Is there anyway to create some sort of catch-all in windows forms applications
that could ensure that no unexpected exceptions bring down an application?

For example, perhaps there is an event that you can watch for whenever an
exception is thrown? If this isnt possible can anyone suggest the best way
to handle unforeseen exceptions? I'm assuming that surrounding huge chunks
of application code in try catch blocks would be a programming faux pas :-)

Thanks to anyone who can offer advice

Kindest Regards

tce
Gabriele G. Ponti - 05 Aug 2005 20:33 GMT
Please see the Application.ThreadException event:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformsapplic
ationclassthreadexceptiontopic.asp

Jay B. Harlow [MVP - Outlook] - 05 Aug 2005 21:10 GMT
tce,
In addition to the other comments:

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/issues/04/06/NET/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.

   Public Shared Sub Main()
       AddHandler Application.ThreadException, AddressOf
Application_ThreadException
       Application.EnableVisualStyles()
       Application.DoEvents()
       Try
           Application.Run(New MainForm)
       Catch ex As Exception
           ' log ex for later diagnosis
           ' optionally show "pretty" version to user
       End Try
   End Sub

   Private Shared Sub Application_ThreadException(ByVal sender As Object,
ByVal e As System.Threading.ThreadExceptionEventArgs)

       ' Log the e.Exception for later diagnosis.
       ' optionally show "pretty" version to user

   End Sub

Hope this helps
Jay

| Hi everyone,
|
[quoted text clipped - 12 lines]
|
| tce
thechaosengine - 06 Aug 2005 16:28 GMT
Thank you so much for your help!

Kindest Regards

tce

> tce,
> In addition to the other comments:
[quoted text clipped - 69 lines]
> |
> | tce

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.