PDA

View Full Version : Difference Between 'On Error GoTo 0' And 'On Error GoTo -1'



Transformer
04-09-2013, 04:27 PM
On Error GoTo 0
It disables the current error handler.So it is used to resume execution normally (without any error handler).


Sub ErrorHandler()

Dim dblValue As Double

On Error Resume Next
ThisWorkbook.Worksheets("A").Range("A1") = "Transformer" ' Subscript out of range error will occur if there is no sheet named "A" in the Workbook
'On Error GoTo 0 'error handler disabled..next error will not be handled.Uncomment the line to check the effect.
dblValue = 1 / 0 ' Division by Zero error
MsgBox "Ingnored all the errors."

End Sub
In above example if one uncomments the line On Error GoTo 0 then it will not handle the error that comes after this statement.

On Error GoTo -1
When an error occurs within a procedure, an exception object is created. On Error GoTo -1 is used to set this exception object to nothing.
If it is not done then next error handler statement (e.g. On Error GoTo errHandler / On Error Resume Next ...) will not work.

To check the effect of not using it comment the line 'On error GoTo -1' in the snippet given below.


Sub ErrorTest()

Dim dblValue As Double

On Error GoTo ErrHandler1
dblValue = 1 / 0
ErrHandler1:
MsgBox "Exception Caught"
On Error GoTo -1 'Comment this line to check the effect
On Error GoTo ErrHandler2
dblValue = 1 / 0
ErrHandler2:
MsgBox "Again caught it."

End Sub

But if you use Resume Next as an error handler then an exception object is not created.


Sub ErrorTest()

Dim dblValue As Double

On Error Resume Next
dblValue = 1 / 0
MsgBox "Exception Caught"
On Error GoTo ErrHandler
dblValue = 1 / 0
'No need to set exception object to nothing as it has not been created
ErrHandler:
MsgBox "Again caught it."

End Sub

More examples to understand the same.



Sub ErrorTestUserHandler()

Dim dblValue As Double

For lngloop = 1 To 10
On Error GoTo ErrHandler
dblValue = 1 / 0
ErrHandler:
MsgBox "Caught it."
On Error GoTo -1 'Comment this line and run it again to see the effect
Next

End Sub



Sub ErrorTestResumeNext()

Dim dblValue As Double

For lngloop = 1 To 10
On Error Resume Next
dblValue = 1 / 0
MsgBox "Caught it."
'No need to set exception object to nothing as it has not been created
Next

End Sub

LalitPandey87
04-11-2013, 08:36 AM
Nice explanation buddy. Really helps us to understand why and how to use error handling in VBA. :cheers:

Excel Fox
04-11-2013, 08:29 PM
excel - Difference between 'on error goto 0' and 'on error goto -1' -- VBA - Stack Overflow (http://stackoverflow.com/questions/14158901/difference-between-on-error-goto-0-and-on-error-goto-1-vba/) should also be helpful


https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)
https://eileenslounge.com/viewtopic.php?p=316705#p316705 (https://eileenslounge.com/viewtopic.php?p=316705#p316705)
https://eileenslounge.com/viewtopic.php?p=316704#p316704 (https://eileenslounge.com/viewtopic.php?p=316704#p316704)
https://eileenslounge.com/viewtopic.php?f=27&t=40919&p=316597#p316597 (https://eileenslounge.com/viewtopic.php?f=27&t=40919&p=316597#p316597)
https://eileenslounge.com/viewtopic.php?p=316412#p316412 (https://eileenslounge.com/viewtopic.php?p=316412#p316412)
https://eileenslounge.com/viewtopic.php?p=316254#p316254 (https://eileenslounge.com/viewtopic.php?p=316254#p316254)
https://eileenslounge.com/viewtopic.php?p=316280#p316280 (https://eileenslounge.com/viewtopic.php?p=316280#p316280)
https://eileenslounge.com/viewtopic.php?p=315915#p315915 (https://eileenslounge.com/viewtopic.php?p=315915#p315915)
https://eileenslounge.com/viewtopic.php?p=315512#p315512 (https://eileenslounge.com/viewtopic.php?p=315512#p315512)
https://eileenslounge.com/viewtopic.php?p=315744#p315744 (https://eileenslounge.com/viewtopic.php?p=315744#p315744)
https://www.eileenslounge.com/viewtopic.php?p=315512#p315512 (https://www.eileenslounge.com/viewtopic.php?p=315512#p315512)
https://eileenslounge.com/viewtopic.php?p=315680#p315680 (https://eileenslounge.com/viewtopic.php?p=315680#p315680)
https://eileenslounge.com/viewtopic.php?p=315743#p315743 (https://eileenslounge.com/viewtopic.php?p=315743#p315743)
https://www.eileenslounge.com/viewtopic.php?p=315326#p315326 (https://www.eileenslounge.com/viewtopic.php?p=315326#p315326)
https://www.eileenslounge.com/viewtopic.php?f=30&t=40752 (https://www.eileenslounge.com/viewtopic.php?f=30&t=40752)
https://eileenslounge.com/viewtopic.php?p=314950#p314950 (https://eileenslounge.com/viewtopic.php?p=314950#p314950)
https://www.eileenslounge.com/viewtopic.php?p=314940#p314940 (https://www.eileenslounge.com/viewtopic.php?p=314940#p314940)
https://www.eileenslounge.com/viewtopic.php?p=314926#p314926 (https://www.eileenslounge.com/viewtopic.php?p=314926#p314926)
https://www.eileenslounge.com/viewtopic.php?p=314920#p314920 (https://www.eileenslounge.com/viewtopic.php?p=314920#p314920)
https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314837#p314837 (https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314837#p314837)
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)

LalitPandey87
04-11-2013, 08:45 PM
excel - Difference between 'on error goto 0' and 'on error goto -1' -- VBA - Stack Overflow (http://stackoverflow.com/questions/14158901/difference-between-on-error-goto-0-and-on-error-goto-1-vba/) should also be helpful

To the point explanation. Before this i also don't even know the use of on error goto -1 and now my confusion is clear. thanks excelfox and transformer.
:cheers:

skywriter
03-28-2015, 09:46 PM
Finally I found an explanation for GoTo 0 that I could understand.
The GoTo -1 and the no exception object with Resume Next made it a 10 out of 10 post.
Thanks so much for posting this!!! :)

DocAElstein
05-27-2015, 12:21 AM
.... the no exception object with Resume Next made it a 10 out of 10 .....

... Agreed. That jem of info was the bit missing by me...now I see ( I think ) why despite an error "occuring", ( andbeing "Handeled by the Resume Next ) I can follow it by enabling another error handler without resetting the exception-- in the case of Resume Next I see now there is no acception raised. It had been explained to me as "surpressing the errror", which I read as something else.. ( not quite sure what - I was puzzeled ) - now I understand ( I think! )

Thanks for Sharing

Alan

DocAElstein
05-27-2015, 08:48 PM
.
. Hi,
. A Couple of follow up questions.

. 1 ) Would you describe On Error GoTo 0 and On Error GoTo -1 as error statements,
And
. 2 ) On Error Resume Next and On Error GoTo some_label/line_number as error handlers.
. Thus we have in VBA 2 types of “error handlers”. I ask as the literature is often confusing, ( possibly caused partly by the GoTo 0 and GoTo -1 which do not actually “go anywhere” I believe ).
. One is often mislead in thinking in terms of 4 “error handlers”

Thanks
Alan

DocAElstein
07-02-2015, 04:07 PM
P.s. Just for info: Similar Blog is here
On Error WTF? | Excel Matters (http://excelmatters.com/2015/03/17/on-error-wtf/)

Edit April 2018
Also see here:
http://www.excelfox.com/forum/showthread.php/2239-Resume-On-Error-GoTo-0-1-GoTo-Error-Handling-Statements-Runtime-VBA-Error-Handling-ORNeRe-GoRoT-N0Nula-1