DevCity.NET - http://devcity.net
How To Handle Unhandled Exceptions
http://devcity.net/Articles/60/1/unhandled_exceptions.aspx
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.

 
by John Spano
Published on 11/23/2002
 
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

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