Results 1 to 8 of 8

Thread: onerror probs

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    77

    onerror probs

    i have this code :
    _________________________________________
    On Error GoTo ErrHandler
    path = GetSetting(App.EXEName, "path", "dir")
    ErrHandler:
    Call errorpad
    ____________________________________
    my program always calls the "errorpad" procedure, no matter what, even if there is no problem (no error should be accured).

    if i write only:
    _______________________________________
    path = GetSetting(App.EXEName, "path", "dir)
    _________________________________________

    then my program is working just fine and no error msgs or debuger are being loaded.
    I guess that means there wasn't a problem in the first place and the "errorpad" shouldnt be load befor.

    Is my vb trying to confuse me?how can i fix this problem?
    VBarak

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    This is normal behaviour.
    If an errro occurs, the execution jumps to the label you specified.
    If no error occurs, the execution jumps to the next line (or where the code guides it to).

    If you use the On Error Goto statement, you need to make sure that the code in the error handler is not executed when no error occurs.

    You can do this in two ways.

    1. By putting an Exit Sub or Exit Function statement before the error handler.

    VB Code:
    1. Sub Test
    2.     On Error GoTo ErrHandler
    3.     path = GetSetting(App.EXEName, "path", "dir")
    4.     Exit Sub
    5. ErrHandler:
    6.     Call errorpad
    7. End Sub
    8.  
    9. Function Test2
    10.     On Error GoTo ErrHandler
    11.     path = GetSetting(App.EXEName, "path", "dir")
    12.     Exit Function
    13. ErrHandler:
    14.     Call errorpad
    15. End Function
    2. By checking Err.Number in your error handler
    VB Code:
    1. Sub Test
    2.     On Error GoTo ErrHandler
    3.     path = GetSetting(App.EXEName, "path", "dir")
    4. ErrHandler:
    5.     If Err.Number <> 0 Then
    6.         Call errorpad
    7.     End if
    8. End Sub

    I prefer the first approach.
    Frans

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    77

    thanks

    the exit sub is not working very well....even if there is an error it exits the sub
    its the opposite problem of what i have had
    ill now go try the other option you gave
    hope itll work!
    VBarak

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Are you sure that there is an error?
    GetSetting doesn't produce an error that fast. Which error do you think you should get?
    Frans

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Actually GetSetting doesn't raise any errors, if the specified key doesn't exist it returns the default value, an empty string in your case.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    I would like to add one more itme of note: If you plan to use errorpad to trap & log errors, then you will/should capture the ErrorNumber before calling it.
    VB Code:
    1. Sub Test
    2.     On Error GoTo ErrHandler
    3.     path = GetSetting(App.EXEName, "path", "dir")
    4.     Exit Sub
    5. ErrHandler:
    6. Dim errNum As Long
    7.     errNum = Err.Number
    8.     Call errorpad errNum
    9. End Sub
    The reason is because the Err object is global in scope, the call to errorpad is sucessful, Err will get reset to no error.
    This has caused a many painful debugging sessionsaround here until we caught on.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    77

    dammmmn

    you right....it does return the defult key if my key is not exist...
    so if that is the case:
    how can i check if the registry key is existed?
    VBarak

  8. #8
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    VB Code:
    1. path = GetSetting(App.EXEName, "path", "dir", "damn, I can't find it.")
    2. If path = "damn, I can't find it." Then
    3.     Msgbox "Nothing there."
    4. End If
    Frans

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