What does the "On Error Resume Next" statement do?
I came to some VBScript examples, and I saw the statement On Error Resume Next
basically at the beginning of the script.
What does it do?
It basically tells the program when you encounter an error just continue at the next line.
It's worth noting that even when On Error Resume Next
is in effect, the Err object is still populated when an error occurs, so you can still do C-style error handling.
On Error Resume Next
DangerousOperationThatCouldCauseErrors
If Err Then
WScript.StdErr.WriteLine "error " & Err.Number
WScript.Quit 1
End If
On Error GoTo 0
When an error occurs, the execution will continue on the next line without interrupting the script.
It means, when an error happens on the line, it is telling vbscript to continue execution without aborting the script. Sometimes, the On Error
follows the Goto
label to alter the flow of execution, something like this in a Sub
code block, now you know why and how the usage of GOTO
can result in spaghetti code:
Sub MySubRoutine() On Error Goto ErrorHandler REM VB code... REM More VB Code... Exit_MySubRoutine: REM Disable the Error Handler! On Error Goto 0 REM Leave.... Exit Sub ErrorHandler: REM Do something about the Error Goto Exit_MySubRoutine End Sub