|
-
Apr 5th, 2002, 04:10 PM
#1
Thread Starter
Addicted Member
Central Error Handlers
I'm thinking of creating a central error handling routine that I can call from all procedures in a project. I'm not sure what the best way to do this would be though.
Should I use a select statement on form name and procedure name??? I could see this getting out of control on most of my projects. If anyone has any comments or suggestions, I would appreciate them.
Here is what I am thinking:
VB Code:
Option Explicit
Public Sub myErr(UserName As String, FormName As String, ProcedureName As String, Optional Comments As String)
Select Case FormName & ProcedureName
Case "Form1" & "Command1_Click()"
If Err.Number = 6 Then
MsgBox "Overflow dumbass"
Exit Sub
End If
Case "Form1" & "Command2_Click()"
If Err.Number = 6 Then
MsgBox "Overflow dumbass"
Exit Sub
End If
End Select
Call logError(UserName, Err.Number, Err.Description, Err.Source, FormName, ProcedureName, Comments)
End Sub
Private Sub logError(UserName As String, ErrNumber As String, ErrDescription As String, ErrSource As String, FormName As String, ProcedureName As String, Optional Comments As String)
Dim errFile As String, fNum As Integer
On Error GoTo errHand
errFile = App.Path & "\Errors.log"
fNum = FreeFile
Open errFile For Append As fNum
Print #fNum, UserName & ", " & Now & ", " & ErrNumber & ", " & ErrDescription & ", " & ErrSource & ", " & FormName & ", " & ProcedureName & ", " & Comments
Close #fNum
MsgBox "Error Number: " & ErrNumber & vbCrLf & vbCrLf & ErrDescription, vbCritical, FormName & " - " & ProcedureName
Exit Sub
errHand:
Close #fNum
MsgBox Err.Number & vbCrLf & Err.Description, , "logError Error"
End Sub
-
Apr 5th, 2002, 04:36 PM
#2
Member
I would be more curious about the way you trap errors in VB code. The VB error object is a tricky thing. It gets changed and emptied without you expecting it. In your code you suppose the original Err.Number still to be there. Are you sure it will be?
-
Apr 5th, 2002, 04:56 PM
#3
Thread Starter
Addicted Member
I was originally passing the different errs through the module (logError was and currently is my default error trapping), but I tested it the other way and it worked for me. However, I can see how the err object could change when being called upon in a different module other than the one the original error was received. (confusing sentence )
I think I will go back to my original format and pass the err object through the myErr routine. Thanks for the feedback!
Anyone else have any ideas?
-
Apr 5th, 2002, 05:01 PM
#4
Lively Member
Me thinks there´s no chance.
Only way is, to handle errors in functions and subs where these errors happen.
Wolf
The goal of Software Engineering is to build something that will last at least until we've finished building it.
-
Apr 5th, 2002, 05:46 PM
#5
Thread Starter
Addicted Member
The code I included does work. Infact, many experts suggest that good practice is to have a central error handling routine for each project. Thanks for your feedback though. Anyone else understand what I am doing???
-
Apr 5th, 2002, 05:58 PM
#6
Lively Member
Uh sorry, but what you are doing is error reporting and logging. Error handling is a try to trap an error and if possible to fix the error condition, hoping to continue the job your app is doing...
And that´s only possible in the subs and functions where there error happend.
Yours
Wolf
The goal of Software Engineering is to build something that will last at least until we've finished building it.
-
Apr 5th, 2002, 08:21 PM
#7
Thread Starter
Addicted Member
I understand that. That's the basic definition or error handling. Are you telling me that this code will not work?
VB Code:
Private Sub Command1_Click()
dim x as integer
on error goto errHand
For x = 0 to 3999999999999
x = x +1
Next
Exit Sub
errHand:
Call myErr(me.name, "Command1_Click", err.number, err.description)
End Sub
Public Sub myErr(FormName As String, ProcedureName As String, errNumber as Long, errDescription as String)
Select Case FormName & ProcedureName
Case "Form1" & "Command1_Click()"
If Err.Number = 6 Then
MsgBox "An Overflow has occured"
Exit Sub
End If
Case "Form1" & "Command2_Click()"
'Put any error handling for command2 here
End Select
msgbox errNumber & vbcrlf & errDescription & vbcrlf & FormName & vbcrlf & ProcedureName
End Sub
You can make myErr a function and pass back a value to tell the calling procedure to take any neccessary actions.
Look at this article:
http://builder.com.com/article.jhtml...htm&src=search
Pay attention to the Error Handling part. Then you might understand what I am talking about.
-
Apr 6th, 2002, 01:50 AM
#8
Thread Starter
Addicted Member
-
Apr 6th, 2002, 01:59 AM
#9
Frenzied Member
Well the way you are doing it seems like way too much work.
Why not pass the name of the form and procedure rather than trying to figure where it came from? Much easier and not much work.
VB Code:
Private Sub Form_Load()
On Error GoTo ErrHandler
'bla bla bla
'bla bla bla
Dim i As Integer
i = 1 / 0
Exit Sub
ErrHandler:
Call ErrorHandler("Form1", "Form_Load", err)
Resume Next
End Sub
'In a module
Public Sub ErrorHandler(frmName As String, procName As String, err As ErrObject)
MsgBox "Error #" & err.Number & " occured in " & frmName & vbCrLf & _
"in the procedure named " & procName & vbCrLf & _
"Description: " & err.Description
End Sub
-
Apr 6th, 2002, 10:52 AM
#10
Thread Starter
Addicted Member
isn't that what i am doing?
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
|