-
Feb 9th, 2018, 12:00 AM
#1
Thread Starter
Lively Member
VB6 Error Handling Question
Hi,
I have a series of functions that I'd like to use Err.Raise on in their error handlers so I can pass errors up the call stack. However, in these functions I have logic structured like so:
Code:
exit_funcName:
'Do Cleanup, Close Recordsets/Objects, etc.
FuncName = someValue
Exit Function
err_funcName:
'Do Some Stuff, Rollback, etc.
Err.Raise Err.Number, , Err.Description <-- This is what I'd like to use
Resume exit_funcName
End Function
However, when I call the Err.Raise method the function seems to exit immediately, which leaves me concerned that my cleanup under exit_funcName may not be taking place. Am I correct in my thinking? And if so, do I need to replicate the code in question under exit_funcName in err_funcName prior to calling Err.Raise? If not what is the proper method to achieve my objective?
Any assistance is (as always) greatly appreciated!
Best Regards
Brad
-
Feb 9th, 2018, 04:10 AM
#2
Re: VB6 Error Handling Question
If I understand the issue, I might put the common code to be done in a local Sub so I could call it where needed.
Code:
exit_funcName:
Gosub cleanUp
FuncName = someValue
Exit Function
err_funcName:
'Do Some Stuff, Rollback, etc.
Gosub cleanUp
Err.Raise Err.Number, , Err.Description <-- This is what I'd like to use
cleanUp:
'Do Cleanup, Close Recordsets/Objects, etc.
Return
End Function
Last edited by passel; Feb 9th, 2018 at 04:17 AM.
-
Feb 9th, 2018, 04:59 AM
#3
Re: VB6 Error Handling Question
This error handling can be hardened a bit more like this:
vb Code:
exit_funcName:
GoSub cleanUp
FuncName = someValue
Exit Function
err_funcName:
Dim vErr As Variant
vErr = Array(Err.Number, Err.Source, Err.Description, Erl)
Resume NextLine
NextLine:
On Error Resume Next
'Do Some Stuff, Rollback, etc.
GoSub cleanUp
On Error GoTo 0
Err.Raise vErr(0), vErr(1), vErr(2) ' <-- This is what I'd like to use
cleanUp:
'Do Cleanup, Close Recordsets/Objects, etc.
Return
End Function
Couple of issue with previous impl:
1. If any of `'Do Some Stuff, Rollback, etc.` and `'Do Cleanup, Close Recordsets/Objects, etc.` calls a function with `On Error ...` error handler then `Err` object contents is reset. So you cannot `Err.Raise Err.Number, ...` after rollback/cleanup. So you have to preserve `Err` object contents *before* performing these -- the `vErr` local var in my snippet.
2. It's possible any of rollback/cleanup code to raise unhandled error and there are two impl options for this case: 1. leave this error propagate to caller or 2. swallow the error, continue cleanup and raise original error. The prev impl behaved like 1. while my snippet implements second strategy.
cheers,
</wqw>
-
Feb 9th, 2018, 07:18 AM
#4
Re: VB6 Error Handling Question
> "when I call the Err.Raise method the function seems to exit immediately, which leaves me concerned that my cleanup under exit_funcName may not be taking place"
It isn't.
At the point you call Err.Raise, VB scans every function in the call stack (including the current one) looking for an error handler and [immediately] jumps to that.
You need to catch the error within the function, (save the values in the Err object), do any clean-up work, then [re-]raise the error [using those saved values; IIRC, the Err object is Type stdErr).
Regards,
Phill W.
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
|