|
-
Feb 3rd, 2004, 09:44 AM
#1
Thread Starter
Junior Member
Error Trapping
Does anybody know how to trap errors that could happen in all the object event procedures (textboxes, pictureboxes, listboxes, etc, etc) in a VB program? Obviously, I don't want to put On Error Goto lines in every single event procedure as I have got thousands of such procedures!
Kind Regards, markunosquirrel
-
Feb 3rd, 2004, 09:55 AM
#2
Re: Error Trapping
Originally posted by markunosquirrel
Obviously, I don't want to put On Error Goto lines in every single event procedure as I have got thousands of such procedures!
better get coding
by any professional standard, you should error trap every routine, other than routines that you know NO POSSIBLE error could occur. For example, I have a routine that converts state abbreviations to the full state name NJ = New Jersey
its a select case routine and there really is no possible way for it to error out, and it has a case else at the end incase a bad abbreviation is passed to it... but any other routine that COULD cause an error, should have an error handle
what you may want to do is write a routine to handle errors and call it from each sub if there is an error
something like below
VB Code:
Private Sub MyRoutine()
On Error Goto EH:
'blah blah blah code here
Exit Sub
EH:
Call HandleError(Err.Number, Err.Description)
End Sub
-
Feb 3rd, 2004, 10:09 AM
#3
Thread Starter
Junior Member
What I'm after is some way of trapping any errors that the program generates that I haven't already trapped. I need to be able to write something like...
On Any Untrapped Error at all in the program Goto Stop_Crash_Error_Handler
The program is very big indeed and being human I may not have written error trapping code when I should have done. I can't look through zillions of lines of code checking it.
The problem that I'm trying to solve is "How can I guarantee that the program doesn't just crash but exits in a tidy manner?"
Regards, Markunosquirrel
-
Feb 3rd, 2004, 10:16 AM
#4
the only way to do what you want is what i just suggested... VB has absolutly no way of having a "GLOBAL" error handle to just accept any error that generates.
When an error occurs the error will travel all the way back up the call stack until an error handler is encountered, if none is then you get the runtime error.
For example, if you have a routine called MySub1 and in this routine you call MySub2. Now lets say MySub1 has an error handler, but MySub2 does not. If an error occurs in MySub2, the error will be sent back to MySub1 and will try to get handled there depeneding on what your error handler code is.
So theoretically... the only way you could have a global error handler is if your entire program was run under 1 routine...
Just a side thought.. perhaps if you had a sub main in a module to start your app, and that had an error handler in it, and you call your main form modally from the sub main, any error that occurs MAY go back to the sub main's error handler.. but I haven't tested that... worth a shot though
-
Feb 3rd, 2004, 10:46 AM
#5
Thread Starter
Junior Member
Many thanks to kleinma for the help. I suspected as much but I will try your suggestion of an On goto in Sub Main. Once again, Many thanks,
Regards, Markunosquirrel.
-
Feb 3rd, 2004, 11:32 AM
#6
If kleinma's suggestion doesn't work for you and you decide to put error handling in most if not all of your routines, I suggest you download MZ-Tools. It is an excellent IDE addin that among many other things allows to add with one click something like the following, even in routines with existing code.
VB Code:
Public Sub SomeProcedure()
'***************************************************************************
'Purpose:
'Inputs :
'Outputs:
'***************************************************************************
On Error GoTo ErrorRoutine
Exit Sub
ErrorRoutine:
DisplayError "SomeProcedure"
End Sub
That's what I do but the format is totally variable. BTW my DisplayError routine looks like the following.
VB Code:
Public Sub DisplayError(strRoutine As String)
'***************************************************************************
'Purpose: Common error display routine
'Inputs: strRoutine - The name of the routine where the error occurred
'Outputs: None
'***************************************************************************
MsgBox ResolveResString(resDisplayError, _
"|1", str$(Err.Number), _
"|2", Err.Source, _
"|3", strRoutine & "." & vbCrLf & vbCrLf, _
"|4", Err.Description), vbExclamation, , Err.HelpFile, Err.HelpContext
End Sub
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
|