Click to See Complete Forum and Search --> : On error twice
nurkht
March 22nd, 2001, 02:33 AM
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:(((
Clearcode
March 22nd, 2001, 03:28 AM
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
Cimperiali
March 22nd, 2001, 05:27 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.