Results 1 to 10 of 10

Thread: Central Error Handlers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2001
    Location
    NJ
    Posts
    148

    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:
    1. Option Explicit
    2.  
    3. Public Sub myErr(UserName As String, FormName As String, ProcedureName As String, Optional Comments As String)
    4.  
    5.   Select Case FormName & ProcedureName
    6.    
    7.     Case "Form1" & "Command1_Click()"
    8.      
    9.       If Err.Number = 6 Then
    10.         MsgBox "Overflow dumbass"
    11.         Exit Sub
    12.       End If
    13.    
    14.     Case "Form1" & "Command2_Click()"
    15.      
    16.       If Err.Number = 6 Then
    17.         MsgBox "Overflow dumbass"
    18.         Exit Sub
    19.       End If
    20.      
    21.   End Select
    22.  
    23.   Call logError(UserName, Err.Number, Err.Description, Err.Source, FormName, ProcedureName, Comments)
    24.  
    25. End Sub
    26.  
    27. 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)
    28.  
    29. Dim errFile As String, fNum As Integer
    30.  
    31.   On Error GoTo errHand
    32.  
    33.   errFile = App.Path & "\Errors.log"
    34.   fNum = FreeFile
    35.      
    36.   Open errFile For Append As fNum
    37.     Print #fNum, UserName & ", " & Now & ", " & ErrNumber & ", " & ErrDescription & ", " & ErrSource & ", " & FormName & ", " & ProcedureName & ", " & Comments
    38.   Close #fNum
    39.  
    40.   MsgBox "Error Number: " & ErrNumber & vbCrLf & vbCrLf & ErrDescription, vbCritical, FormName & " - " & ProcedureName
    41.  
    42.   Exit Sub
    43.  
    44. errHand:
    45.  
    46.   Close #fNum
    47.   MsgBox Err.Number & vbCrLf & Err.Description, , "logError Error"
    48.  
    49. End Sub



    ADO, SQL, Access, HTML, ASP, XML
    Visual Basic 6.0 SP5 Enterprise Edition
    VB.Net


  2. #2
    Member
    Join Date
    Mar 2002
    Location
    Amsterdam
    Posts
    51
    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?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2001
    Location
    NJ
    Posts
    148
    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?



    ADO, SQL, Access, HTML, ASP, XML
    Visual Basic 6.0 SP5 Enterprise Edition
    VB.Net


  4. #4
    Lively Member knochenfish's Avatar
    Join Date
    Apr 2001
    Location
    Cologne
    Posts
    77

    Unhappy

    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2001
    Location
    NJ
    Posts
    148
    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???



    ADO, SQL, Access, HTML, ASP, XML
    Visual Basic 6.0 SP5 Enterprise Edition
    VB.Net


  6. #6
    Lively Member knochenfish's Avatar
    Join Date
    Apr 2001
    Location
    Cologne
    Posts
    77
    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2001
    Location
    NJ
    Posts
    148
    I understand that. That's the basic definition or error handling. Are you telling me that this code will not work?

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. dim x as integer
    4.  
    5.   on error goto errHand
    6.  
    7.   For x = 0 to 3999999999999
    8.     x = x +1
    9.   Next
    10.  
    11.   Exit Sub
    12.  
    13. errHand:
    14.  
    15.   Call myErr(me.name, "Command1_Click", err.number, err.description)
    16.  
    17. End Sub
    18.  
    19. Public Sub myErr(FormName As String, ProcedureName As String, errNumber as Long, errDescription as String)
    20.  
    21.   Select Case FormName & ProcedureName
    22.    
    23.     Case "Form1" & "Command1_Click()"
    24.      
    25.       If Err.Number = 6 Then
    26.         MsgBox "An Overflow has occured"
    27.         Exit Sub
    28.       End If
    29.    
    30.     Case "Form1" & "Command2_Click()"
    31.      
    32.       'Put any error handling for command2 here
    33.      
    34.   End Select
    35.  
    36.   msgbox errNumber & vbcrlf &  errDescription & vbcrlf &   FormName & vbcrlf &   ProcedureName
    37.  
    38. 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.



    ADO, SQL, Access, HTML, ASP, XML
    Visual Basic 6.0 SP5 Enterprise Edition
    VB.Net


  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2001
    Location
    NJ
    Posts
    148
    any one?



    ADO, SQL, Access, HTML, ASP, XML
    Visual Basic 6.0 SP5 Enterprise Edition
    VB.Net


  9. #9
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    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:
    1. Private Sub Form_Load()
    2.     On Error GoTo ErrHandler
    3.     'bla bla bla
    4.     'bla bla bla
    5.     Dim i As Integer
    6.     i = 1 / 0
    7.     Exit Sub
    8. ErrHandler:
    9.     Call ErrorHandler("Form1", "Form_Load", err)
    10.     Resume Next
    11. End Sub
    12.  
    13. 'In a module
    14. Public Sub ErrorHandler(frmName As String, procName As String, err As ErrObject)
    15.     MsgBox "Error #" & err.Number & " occured in " & frmName & vbCrLf & _
    16.            "in the procedure named " & procName & vbCrLf & _
    17.            "Description: " & err.Description
    18. End Sub

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2001
    Location
    NJ
    Posts
    148
    isn't that what i am doing?



    ADO, SQL, Access, HTML, ASP, XML
    Visual Basic 6.0 SP5 Enterprise Edition
    VB.Net


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