Need help with error handling procedure please :)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Need help with error handling procedure please :)

  1. #1
    Join Date
    Jun 2009
    Posts
    10

    Need help with error handling procedure please :)

    Currently I have an error handler that reports the error and details of it: logs some information regarding it (details, line number of the error, location *procedure name* of the error, operating system the error occurred on, date the error happened. The code I used to create this is listed below. After reading several articles on error trapping and logging I discovered one that suggested dumping all the active variables of the procedure and their values in the log as well. It seemed to indicate that there was a way to do this without searching through every procedure on every form and listing the variables manually for the log. So my question is is there a way to have my error handler when outputting simply stick in the names of the active variables and values without explicitly setting what variables it will write before hand? If further clarification is needed please let me know. Here's an example of what I want my log to look like.
    ----------------------5gws V2 Error Log------------------------
    Please report errors on the forums by copy pasting this log in
    the trouble shooting section on the forums. These are found at
    the following URL address:
    http://www.blackdragongames.co.uk/fo...p?showforum=25
    After posting feel free to delete the contents of this log
    or simply delete the log file itself if you want.
    ----------------------5gws V2 Error Log------------------------


    *********
    5gws version 1.0
    Error Details: Error #380 - Invalid property value! This error occurred in frmDatabaseWrestler.Loadwrestler on line #305.
    OS Core: Windows_NT
    Date of error: 4/12/2009 12:13:53 AM

    ~Active Variables~
    ActiveVariableName1="ASDF THIS VARIABLE HAS STRING DATA"
    ActiveVariableName2=╪'≡E
    ActiveVariableName3=1345
    *********
    In this example scenario the log created shows all relevant information including a list of active variables. Looking at the log if i know that variable 2 was suppose to be an integer I can tell clearly that this specifically was where the error was instigated.

    Here's what I have for error handling currently:
    At the top of the form or module - Rename the "FORMNAME" with the name of the form or database
    Code:
    Private Const MODULE_NAME As String = "FORMNAME." '//For error trapping
    At the start of the procedure or function - Replace PROCEDURE_NAME witht he name of the procedure or funciton
    Code:
      'Set up Error handler.
       On Error GoTo handleError:
       Dim sErrorDescription As String
       Const sProcSig = MODULE_NAME & "PROCEDURE_NAME"
    At the end of the function
    Code:
    '//Error trapping
    Exit Sub
    handleError:
        With Err
            sErrorDescription = "Error #" & .Number & " - " & _
            .Description & "! This error occurred in " & sProcSig & _
            IIf(Erl <> 0, " on line #" & CStr(Erl) & ".", ".")
        End With
       ErrorHandler sErrorDescription
    Resume Next '//Continue on the next line of code
    Error handler procedures in a module
    Code:
    Public Sub ErrorHandler(sErrorDescription As String)
        Dim response As String
        response = MsgBox(sErrorDescription & vbCrLf & vbCrLf & " ~5gws will now create a log of this error in your 5gws directory for easy reporting. Code execution will continue on the next line of code. We highly recomend reporting the bug so it can get fixed and does not cause problems in yours or others database / game. Corrupted data can lead to an unusable save game.~", , App.Title & " Error")
        '//Create log file entry
        LogError sErrorDescription
    End Sub
    Code:
    Public Sub LogError(Description As String)
        '//Dimension variables for use
        Dim ff As Integer
        Dim filepath As String
        
        '//Set variable inital values
        ff = FreeFile
        filepath = App.Path & "/ErrorLog.txt"
        
        '//create log file if it doesnt exist
        If Dir$(filepath) = "" Then
            Open filepath For Output As #ff
            Print #ff, "----------------------5gws V2 Error Log------------------------"
            Print #ff, "Please report errors on the forums by copy pasting this log in "
            Print #ff, "the trouble shooting section on the forums. These are found at "
            Print #ff, "the following URL address:                                     "
            Print #ff, "http://www.blackdragongames.co.uk/forum/index.php?showforum=25 "
            Print #ff, "After posting feel free to delete the contents of this log     "
            Print #ff, "or simply delete the log file itself if you want.              "
            Print #ff, "----------------------5gws V2 Error Log------------------------"
            Print #ff, " "
            Print #ff, " "
            Close #ff
        End If
          
        ff = FreeFile
        Open filepath For Append As #ff
        Print #ff, "*********"
        Print #ff, "5gws version " & App.Major & "." & App.Minor
        Print #ff, "Error Details: " & Description
        Print #ff, "OS Core: " & Environ$("os")
        Print #ff, "Date of error: " & Now()
        Print #ff, "*********"
        Print #ff, " "
        Print #ff, " "
        Close #ff
        
    End Sub

    Can anyone help this would be of massive help to me.

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Need help with error handling procedure please :)

    Sorry, no magic pill. If you want to trap errors, you have to set the traps. Errors bubble-up, so you should put the most relevant info into the bottom layers.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2009
    Posts
    10

    Re: Need help with error handling procedure please :)

    Any chance you have seen any good tutorials on error trapping around? What I've come up with above took me about a week or looking around but I don't think I understand alot of the basics of the concepts of error trapping. I'd hoped to be able to dump the variable data...at least thus far I have it so it tells me the exact line the error occured in but the game i'm making is HEAVILY data driven so the data is as important as the line as a corrupted file / record can make the error impossible to recreate unless you have the user data..I was hoping to get the variable data in order to understand where things go wrong...I'll just have to do it procedure by procedure then though...anyway I'm rambling now as it's late :-D...but any chance of being pointed toward some good info on error handling? Thanks in advance for the help .

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Need help with error handling procedure please :)

    Take a look at this article with code
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Need help with error handling procedure please :)

    Two things about error handling:

    The scope of an error trap like On Error Goto lies only within a sub or function.
    To find out exactly where the error came up you would have to have traps in every function or sub and write something to log the relevant variables of this sub.
    This is rather uncomfortable and also unpredictive because you never know in advance which data would be relevant for an error.

    An optimal error handling largely depends on the programming you have done.
    You have to set error traps at points where you KNOW an error could happen and also know WHAT KIND of error could happen there.
    E.g. when attempting to read a file you would know a possible error is, the file does not exist.
    So you could trap that exception within the reading sub.
    (I admit this is no perfect example, because you better direct your programming such as you first verify file existence for attempting to read).

  6. #6
    Join Date
    Jun 2009
    Posts
    10

    Re: Need help with error handling procedure please :)

    I'm doing as much pre-emptive trapping as I can so I guess that's what will help the most. Thanks for the info guys.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center