Results 1 to 7 of 7

Thread: Create a "catch all" Exception handler

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Create a "catch all" Exception handler

    This will catch any exception that you do not explicitly handle. Not only can you gather information about the state of the application when the exception is thrown, but it also gives you a chance to shut down the app cleanly.

    VB Code:
    1. Imports System.Threading
    2.  
    3. Public Class frmMain
    4.     Inherits System.Windows.Forms.Form
    5.  
    6.     '//Windows Form Designer generated code
    7.  
    8.     Public Shared Sub Main()
    9.         Dim frm As New frmMain
    10.  
    11.         'Install a ThreadException Handler
    12.         AddHandler Application.ThreadException, _
    13.                    New ThreadExceptionEventHandler(AddressOf frm.GlobalErrorProc)
    14.  
    15.         'Run the main form
    16.         Application.Run(frm)
    17.  
    18.     End Sub
    19.  
    20.     Public Sub GlobalErrorProc(ByVal sender As Object, _
    21.                                ByVal args As ThreadExceptionEventArgs)
    22.  
    23.         'Inform the user of the exception, and allow the application to close nicely
    24.         MessageBox.Show("An unhandled exception has occured: " & args.Exception.Message, _
    25.                         "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
    26.         Me.Close()
    27.         Application.Exit()
    28.  
    29.         'You could also write the stack trace out to a file or something at this for
    30.         'debugging purposes, or gather any other state info that you want
    31.  
    32.     End Sub
    33.  
    34.     Private Sub Button1_Click(ByVal sender As Object, _
    35.                               ByVal e As System.EventArgs) Handles Button1.Click
    36.         'Just to test it out, throw an exception
    37.         Throw New Exception("This is a test")
    38.     End Sub
    39.  
    40. End Class
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    Addicted Member Codehammer's Avatar
    Join Date
    Aug 2004
    Posts
    164

    Re: Create a "catch all" Exception handler

    I Thought dim ex as Exception caught Everything!!

    Worked Fine all These years.
    Curiosity SKILLED the cat
    Google Talk from your Mobile phone

    Chat from your mobile or get an emulator like J2ME Wireless Toolkit 2.2

  3. #3
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Create a "catch all" Exception handler

    Quote Originally Posted by Codehammer
    I Thought dim ex as Exception caught Everything!!

    Worked Fine all These years.
    this example calls the GlobalErrorProc sub whenever an error occurs, so you dont have to have many try/catch statements.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Create a "catch all" Exception handler

    How about adding a feature like "Abort,Retry,Ignore"?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Create a "catch all" Exception handler

    If this exception catches an error, it is already too late to try to continue on with any code, as there were no try/catches down below. Really, you should use try/catches where you can and use this handler for situations you didn't forsee crashing (as a safety net).

  6. #6
    Hyperactive Member OMITT3D's Avatar
    Join Date
    Mar 2006
    Posts
    368

    Re: Create a "catch all" Exception handler

    It didn't work for me :'( it just threw the exception no msgbox no nothing =\

  7. #7
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Create a "catch all" Exception handler

    It's best to use Sub Main to launch your application and put that in a Try and Catch statement. That way, if an error occurs somewhere in your program you're guarenteed to catch it and do whatever you want with it.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width