|
-
Apr 15th, 2003, 09:01 AM
#1
Thread Starter
Lively Member
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?
-
Apr 15th, 2003, 09:09 AM
#2
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:
Sub Test
On Error GoTo ErrHandler
path = GetSetting(App.EXEName, "path", "dir")
Exit Sub
ErrHandler:
Call errorpad
End Sub
Function Test2
On Error GoTo ErrHandler
path = GetSetting(App.EXEName, "path", "dir")
Exit Function
ErrHandler:
Call errorpad
End Function
2. By checking Err.Number in your error handler
VB Code:
Sub Test
On Error GoTo ErrHandler
path = GetSetting(App.EXEName, "path", "dir")
ErrHandler:
If Err.Number <> 0 Then
Call errorpad
End if
End Sub
I prefer the first approach.
-
Apr 15th, 2003, 10:38 AM
#3
Thread Starter
Lively Member
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!
-
Apr 15th, 2003, 10:49 AM
#4
Are you sure that there is an error?
GetSetting doesn't produce an error that fast. Which error do you think you should get?
-
Apr 15th, 2003, 11:50 AM
#5
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.
-
Apr 15th, 2003, 12:31 PM
#6
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:
Sub Test
On Error GoTo ErrHandler
path = GetSetting(App.EXEName, "path", "dir")
Exit Sub
ErrHandler:
Dim errNum As Long
errNum = Err.Number
Call errorpad errNum
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.
-
Apr 15th, 2003, 01:51 PM
#7
Thread Starter
Lively Member
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?
-
Apr 15th, 2003, 05:59 PM
#8
VB Code:
path = GetSetting(App.EXEName, "path", "dir", "damn, I can't find it.")
If path = "damn, I can't find it." Then
Msgbox "Nothing there."
End If
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
|