VB6 Error Handling Question-VBForums
Results 1 to 4 of 4

Thread: VB6 Error Handling Question

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2014
    Posts
    42

    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

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,181

    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.

  3. #3
    Hyperactive Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    417

    Re: VB6 Error Handling Question

    This error handling can be hardened a bit more like this:
    vb Code:
    1. exit_funcName:
    2.     GoSub cleanUp
    3.     FuncName = someValue
    4.     Exit Function
    5. err_funcName:
    6.     Dim vErr As Variant
    7.     vErr = Array(Err.Number, Err.Source, Err.Description, Erl)
    8.     Resume NextLine
    9. NextLine:
    10.     On Error Resume Next
    11.     'Do Some Stuff, Rollback, etc.
    12.     GoSub cleanUp
    13.     On Error GoTo 0
    14.     Err.Raise vErr(0), vErr(1), vErr(2) ' <-- This is what I'd like to use
    15.  
    16. cleanUp:
    17.     'Do Cleanup, Close Recordsets/Objects, etc.
    18.     Return
    19. 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>

  4. #4
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    590

    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
  •  



Featured


Click Here to Expand Forum to Full Width