Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  How To Handle Unhandled Exceptions
How To Handle Unhandled Exceptions
by John Spano | Published  11/23/2002 | .NET Framework | Rating:
John Spano

John Spano cofounder and CTO of NeoTekSystems, a Greenville, South Carolina technology consulting company. NeoTekSystems offers IT consulting, custom programming, web design and web hosting. We specialize in Microsoft .Net enterprise development and business design.

I have six years of experience in software architecture. My primary focus is on Microsoft technologies, and I have been involved in .NET since beta 1. I currently hold a MCSD certification, 2 MCTS's (Windows, Web) a MCPD in Distributed, 2 MCITP's, a Microsoft MVP, and have won the Helper of the Month contest for July 2002 in the devCity.NET forums.

Corporate URL: www.NeoTekSystems.com
Primary email: JSpano@NeoTekSystems.com
Alternate email: Jspano@devcity.net.

 

View all articles by John Spano...
How To Handle Unhandled Exceptions

VB.NET has a cool feature that lets you catch any unhandled exception in your programs. To set up this functionality, you have to add 2 subs that handle the exceptions, and 2 event handler definitions.

Code:

'The 2 event handlers
  'add an unhandled exceptions handler
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
  'for regular unhandled stuff
AddHandler currentDomain.UnhandledException, AddressOf MYExceptionHandler
  'for threads behind forms
AddHandler Application.ThreadException, AddressOf MYThreadHandler

Now .NET will call the 2 above functions when an Exception occurs and you don't have a Try..Catch block.

Code:
'The 2 functions
Private Sub MYExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
  'What I normally do here is call a form I have created that resembles the Microsoft Unhandled
  'Exceptions Form. It says sorry for the blow up, an error report was created, do you want to
  'send it to my company etc. You can get information off of the variable e for the error report.
End Sub

Private Sub UHThreadEX(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
  'What I normally do here is call a form I have created that resembles the Microsoft Unhandled
  'Exceptions Form. It says sorry for the blow up, an error report was created, do you want to
  'send it to my company etc. You can get information off of the variable e for the error report.
End Sub

To test use this:

Code:
Module Mod1

  Public Sub Main
    'The 2 event handlers
    'add an unhandled exceptions handler
    Dim currentDomain As AppDomain = AppDomain.CurrentDomain
    'for regular unhandled stuff
    AddHandler currentDomain.UnhandledException, AddressOf MYExceptionHandler
    'for threads behind forms
    AddHandler Application.ThreadException, AddressOf MYThreadHandler

    Dim X as Integer
    X = 5
    X = X / 0 'throws exception will be caught by subs below.
  End Sub

  Private Sub MYExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
    Dim EX As Exception
    EX = e.ExceptionObject
    Console.WriteLine(EX.StackTrace)
  End Sub

  Private Sub MYThreadHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
    Console.WriteLine(e.Exception.StackTrace)
  End Sub
End Module

This article was originally posted as devCity.NET Forums FAQ - http://www.devcity.net/forums/faq.asp?fid=15#TID5038

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.84210526315787 out of 5
 76 people have rated this page
Article Score35295
Sponsored Links