CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: On error twice

  1. #1
    Join Date
    Aug 2000
    Location
    Moscow
    Posts
    85

    On error twice

    Hello

    When I try hook error in a procedure, I write
    "On error goto" and error is hooked

    But when I try to make two error handlers by
    "on error" which I write twice, error in second case is not hooked.

    Why?

    I write "on error" in two places and after second "on error" error is not hooked((






  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: On error twice

    When an error is triggered in VB, the system searches back through the call stack to find the first error handler and invokes this.
    In order to cause the error to "bubble up" to the next error handler you need to raise it again from within that error handler thus:

    on error Goto FirstHandler

    FirstHandler:
    'some code here....
    '\\ Bubble up the error
    With Err
    .Raise .Number, .Description, .Source
    End With




    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: On error twice

    On error goto myErr
    ...
    exit sub
    myErr:
    'pass err to myfirsterrHandler storing it in a variable!
    resume myfirsterrHandler
    exit sub
    myfirsterrhandelr:
    on error goto myseconderrhandler
    'examine your err variable
    ....
    exit sub
    myseconderrhandler:
    'you can trap second error here
    ...
    end sub
    'Story is: to trap a second error in a different
    'err_handler procedure in same sub, you need to reset errors
    Not simply do err.clear (it won't work) but resume something.
    Another way to do it is:
    private sub my_sub
    on error goto aaa
    ....
    'now at this point you want to trap error somewhere else
    'if it happens here.
    on erroro goto 0 'disable first error handling
    on error goto bbbb 'enable another error handling
    ...
    exit sub
    aaa:
    ...
    exit sub
    bbbb:
    ....
    end sub
    Hope this help (let me know)

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured