0

What is the best way in VB6 to pass an error back to the calling function?

1    On Error Resume Next
2    ' do something
3    If Err.Number <> 3026 Or Err <> 0 Then ?????????

How would you send the error in Line 3 back to the calling function? Is the following the only way to achieve this?

errNum = Err.Number
On Error Goto 0    
Err.Raise errNum
CJ7
  • 22,579
  • 65
  • 193
  • 321

4 Answers4

2

Use On Error GoTo and re-raise the error in the handler with Err.Raise.

Private Function DoSomething(ByVal Arg as String)

    On Error GoTo Handler

    Dim ThisVar as String
    Dim ThatVar as Long

    ' Code here to implement DoSomething...

    Exit Function

Handler:
    Err.Raise Err.Number, , "MiscFunctions.DoSomething: " & Err.Description

End Function

You'll then be able to get the error number and description in the caller via Err.Number and Err.Description.

If the caller is also using On Error GoTo, you'll see them in the handler there.

If the caller is using On Error Resume Next, then you can still use those same variables inline.

I prefer the first option, using On Error Goto in all functions and subs, because it seems like the natural way to use VB6's built-in error raising features. You can also update the description in the called function's handler, like the example above, and get a pseudo call stack you can eventually log or display to yourself during debugging.

More VB6 error handling thoughts here:

Is it possible to retrieve the call stack programmatically in VB6?

How to clean up error handling in a function?

Community
  • 1
  • 1
JeffK
  • 3,019
  • 2
  • 26
  • 29
0

Why not add ByRef errorCode as Long to the called function's args and set it equal to Err.Number after ' do something

Or you could have a public field called ErrorCode as Long that you could set after ' do something

I have worked with a lot of industrial control APIs and both of these methods have been used.

djv
  • 15,168
  • 7
  • 48
  • 72
  • How in that case would the calling function's error handler be triggered? – CJ7 Jan 16 '13 at 00:00
  • I guess I misunderstood. Try `On Error Goto 0` after `' do something` to restore the error handling of the calling function. You ignored it with `On Error Resume Next` – djv Jan 16 '13 at 03:42
  • Yes but how do I re-raise the error so the calling function handles the error? The error has already been handled by `On Error Resume Next`, so it would need to be raised again. – CJ7 Jan 16 '13 at 04:00
  • Try this: replace `On Error Resume Next` with `On Error Goto ErrHandler` Have an error handler that stores the `Err.Number`, `Err.source`, `Err.Description` information, then does a `Resume`. When you are ready to exit the method, do an `Err.Raise` using the information stored before as arguments. Sorry I don't have VB6 installed anymore. I hope that works. – djv Jan 16 '13 at 04:15
0

You can easily send the error to the upper (calling) sub/function as long as the function that raises the error does not have (ON ERROR RESUME ---), that way, error handling is left in the upper level only. Otherwise you will have to handle the error inside the called function

Private Sub Command1_Click()
     Dim test As Integer
     On Error Resume Next
     test = myFunction     'Calling a function that is known to have an error
     If Err <> 0 Then
         MsgBox "MyFunction failed because:" & Err.Description 'Error is passed
     End If

End Sub

'--------------------------    

Function myFunction() As Integer
    Dim i As Integer
    i = 1
    i = 4 / 0   'This will raise an Error, and control returns to the calling sub
    i = 2       'This will never get executed
    myFunction = i
End Function
Ahmad
  • 12,336
  • 6
  • 48
  • 88
0

If you simply want to pass the error back to the original caller without handling it, then you want to remove any ON ERROR in the child function:

Public Sub ParentSub()
    On Error GoTo ErrorHandler
    ' do something
    Call ChildSub()
    ' do something
    Exit Sub
ErrorHandler:
    ' handle the error here
End Sub

Public Sub ChildSub()
    ' do something
    ' if there is an error here, the error will be handled in ErrorHandler of ParentSub
End Sub

or if you want to handle it in both subs:

Public Sub ParentSub()
    On Error GoTo ErrorHandler
    ' do something
    Call ChildSub()
    ' do something
    Exit Sub
ErrorHandler:
    ' handle the error here
End Sub

Public Sub ChildSub()
    On Error GoTo ErrorHandler
    ' do something
   Exit Sub
ErrorHandler:
    ' handle the error here and pass it back to the ParentSub to handle it as well
    Err.Raise Err.Number
End Sub
George
  • 2,165
  • 2
  • 22
  • 34