What does the "On Error Resume Next" statement do?

VbscriptError Handling

Vbscript Problem Overview


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?

Vbscript Solutions


Solution 1 - Vbscript

It basically tells the program when you encounter an error just continue at the next line.

Solution 2 - Vbscript

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

Solution 3 - Vbscript

When an error occurs, the execution will continue on the next line without interrupting the script.

Solution 4 - Vbscript

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

Solution 5 - Vbscript

On Error Statement - Specifies that when a run-time error occurs, control goes to the statement immediately following the statement. How ever Err object got populated.(Err.Number, Err.Count etc)

Solution 6 - Vbscript

It enables error handling. The following is partly from https://msdn.microsoft.com/en-us/library/5hsw66as.aspx

' Enable error handling. When a run-time error occurs, control goes to the statement 
' immediately following the statement where the error occurred, and execution
' continues from that point.
On Error Resume Next

SomeCodeHere

If Err.Number = 0 Then
    WScript.Echo "No Error in SomeCodeHere."
Else
  WScript.Echo "Error in SomeCodeHere: " & Err.Number & ", " & Err.Source & ", " & Err.Description
  ' Clear the error or you'll see it again when you test Err.Number
  Err.Clear
End If

SomeMoreCodeHere

If Err.Number <> 0 Then
  WScript.Echo "Error in SomeMoreCodeHere:" & Err.Number & ", " & Err.Source & ", " & Err.Description
  ' Clear the error or you'll see it again when you test Err.Number
  Err.Clear
End If

' Disables enabled error handler in the current procedure and resets it to Nothing.
On Error Goto 0

' There are also `On Error Goto -1`, which disables the enabled exception in the current 
' procedure and resets it to Nothing, and `On Error Goto line`, 
' which enables the error-handling routine that starts at the line specified in the 
' required line argument. The line argument is any line label or line number. If a run-time 
' error occurs, control branches to the specified line, making the error handler active. 
' The specified line must be in the same procedure as the On Error statement, 
' or a compile-time error will occur.

Solution 7 - Vbscript

On Error Resume Next means that On Error, It will resume to the next line to resume.

e.g. if you try the Try block, That will stop the script if a error occurred

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCarlos BlancoView Question on Stackoverflow
Solution 1 - VbscriptDavidView Answer on Stackoverflow
Solution 2 - VbscriptTmdeanView Answer on Stackoverflow
Solution 3 - VbscriptPierre-Alain VigeantView Answer on Stackoverflow
Solution 4 - Vbscriptt0mm13bView Answer on Stackoverflow
Solution 5 - VbscriptChandralalView Answer on Stackoverflow
Solution 6 - VbscriptalfredoView Answer on Stackoverflow
Solution 7 - VbscriptLunarHunterView Answer on Stackoverflow