Some extra notes for a few Posts
http://www.eileenslounge.com/viewtop...278892#p278892
https://www.myonlinetraininghub.com/...andling-in-vba
https://excelfox.com/forum/showthrea...GoRoT-N0Nula-1
https://excelfox.com/forum/showthrea...0559#post10559
On Error GoTo -1 is not equivalent of using Err.Clear. It does ( also) clear the error object, (equivalent of using Err.Clear ).
On Error GoTo -1 takes Excel out of the so called “exception state”. It also does clear the Err object registers, (equivalent of using Err.Clear ). But the later is secondary to its main function of “clearing the exception”.
The next macro has 3 identical erroring code lines . Just before each error we have an error handler, which we might expect would trap the error following it . All three error handlers are similar and are of the type On Error GoTo [LABEL] But we find that only the first two error handlers work….
In this macro the first and the second error handlers, of the type On Error GoTo [LABEL] are enabled, and so when an error occurs the coding jumps to the appropriate Label
The second Error handler would not have worked, that is to say the second error would not have been trapped without the code line of On Error GoTo -1 . On Error GoTo -1 has cleared the exception state.
The third error handler, also of the type On Error GoTo [LABEL], does not work. It does not work, that is to say the error is not trapped , because we are in the exception state. One of the characteristics of the exception state is that any attempt to enable an error handler will be ignored. Another characteristic of the exception state is that any enabled error handler, ( in this case the second one ) , will also be ignored.
It is also sometimes said in this situation that the second error handler is active and is still handling the second error. It cannot handle another error , and any further errors will be handled by the VBA default error handler
The following other error things also , in addition to their main function, clear the Err object registers –Code:Sub OnErrorGoToMinus1_takes_Excel_out_of_the_so_called_exception_state() ' It also does clear the Err object registers, (equivalent of using Err.Clear ). But the later is secondary to its main function of "clearing the exception" Dim Rslt As Double On Error GoTo ErrHndlr1 Let Rslt = 1 / 0 ' This error gets trapped by ErrHndlr1 MsgBox Prompt:="You will never see this", Title:="You will never see this" Exit Sub ' You will never come here in this demo macro, but its good practice to get in the habit of always doing this exit sub ErrHndlr1: Debug.Print Err.Number & vbCr & vbLf & Err.Description ' 11 Division durch Null On Error GoTo -1 ' the next line will give us no error infomation because the On Error GoTo -1 has cleared the Err object registers Debug.Print Err.Number & vbCr & vbLf & Err.Description ' 0 On Error GoTo ErrHndlr2 ' the main function of On Error GoTo -1 is to "clear the exception" which means this second error hanhler will work Let Rslt = 1 / 0 ' This error gets trapped by ErrHndlr2 MsgBox Prompt:="You will never see this", Title:="You will never see this" Exit Sub ' You will never come here in this demo macro, but its good practice to get in the habit of always doing this exit sub ErrHndlr2: Debug.Print Err.Number & vbCr & vbLf & Err.Description ' 11 Division durch Null ' I will not do On Error GoTo -1 and see what happens... On Error GoTo ErrHndlr3 Let Rslt = 1 / 0 ' This will be handled by the VBA default error handler: The error will not be trapped by the second error handler , ErrHndlr2 MsgBox Prompt:="You will never see this", Title:="You will never see this" Exit Sub ' You will never come here in this demo macro, but its good practice to get in the habit of always doing this exit sub ErrHndlr3: ' You will never come here. The third error is not trapped: It will be handled by the VBA default error handler End Sub
_ On Error GoTo 0 ,
_ changing the error handler
_ Resume, ( Resume; Resume Next; Resume [label] )
-.....see next post




Reply With Quote
Bookmarks