|
-
Jun 15th, 2004, 07:19 AM
#1
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:
Imports System.Threading
Public Class frmMain
Inherits System.Windows.Forms.Form
'//Windows Form Designer generated code
Public Shared Sub Main()
Dim frm As New frmMain
'Install a ThreadException Handler
AddHandler Application.ThreadException, _
New ThreadExceptionEventHandler(AddressOf frm.GlobalErrorProc)
'Run the main form
Application.Run(frm)
End Sub
Public Sub GlobalErrorProc(ByVal sender As Object, _
ByVal args As ThreadExceptionEventArgs)
'Inform the user of the exception, and allow the application to close nicely
MessageBox.Show("An unhandled exception has occured: " & args.Exception.Message, _
"Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Close()
Application.Exit()
'You could also write the stack trace out to a file or something at this for
'debugging purposes, or gather any other state info that you want
End Sub
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'Just to test it out, throw an exception
Throw New Exception("This is a test")
End Sub
End Class
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 10th, 2005, 08:13 AM
#2
Addicted Member
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
-
Oct 12th, 2005, 02:26 AM
#3
Re: Create a "catch all" Exception handler
 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.
-
Oct 12th, 2005, 02:45 AM
#4
Re: Create a "catch all" Exception handler
How about adding a feature like "Abort,Retry,Ignore"?
-
Nov 8th, 2005, 09:33 AM
#5
Fanatic Member
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).
-
May 9th, 2006, 04:20 PM
#6
Hyperactive Member
Re: Create a "catch all" Exception handler
It didn't work for me :'( it just threw the exception no msgbox no nothing =\
-
May 11th, 2006, 10:25 PM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|