VBScript -- Using error handling

vbscript on error goto
vbscript on error exit
vbscript error handling line number
vbscript on error resume next
vbscript on error goto label
vbscript error object
vbscript goto
vbscript try-catch

I want to use VBScript to catch errors and log them (ie on error "log something") then resume the next line of the script.

For example,

On Error Resume Next
'Do Step 1
'Do Step 2
'Do Step 3

When an error occurs on step 1, I want it to log that error (or perform other custom functions with it) then resume at step 2. Is this possible? and how can I implement it?

EDIT: Can I do something like this?

On Error Resume myErrCatch
'Do step 1
'Do step 2
'Do step 3

myErrCatch:
'log error
Resume Next

VBScript has no notion of throwing or catching exceptions, but the runtime provides a global Err object that contains the results of the last operation performed. You have to explicitly check whether the Err.Number property is non-zero after each operation.

On Error Resume Next

DoStep1

If Err.Number <> 0 Then
  WScript.Echo "Error in DoStep1: " & Err.Description
  Err.Clear
End If

DoStep2

If Err.Number <> 0 Then
  WScript.Echo "Error in DoStop2:" & Err.Description
  Err.Clear
End If

'If you no longer want to continue following an error after that block's completed,
'call this.
On Error Goto 0

The "On Error Goto [label]" syntax is supported by Visual Basic and Visual Basic for Applications (VBA), but VBScript doesn't support this language feature so you have to use On Error Resume Next as described above.

Error Handling - VBScript in a Nutshell [Book], Ordinarily, all runtime errors that are generated by the VBScript engine are fatal, since execution of the current script is halted when the error occurs. Error handling allows you to inform the user of the problem and either halt execution of the program or, if it is prudent, continue executing the program. VBScript - Error Handling - There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors.

Note that On Error Resume Next is not set globally. You can put your unsafe part of code eg into a function, which will interrupted immediately if error occurs, and call this function from sub containing precedent OERN statement.

ErrCatch()

Sub ErrCatch()
    Dim Res, CurrentStep

    On Error Resume Next

    Res = UnSafeCode(20, CurrentStep)
    MsgBox "ErrStep " & CurrentStep & vbCrLf & Err.Description

End Sub

Function UnSafeCode(Arg, ErrStep)

    ErrStep = 1
    UnSafeCode = 1 / (Arg - 10)

    ErrStep = 2
    UnSafeCode = 1 / (Arg - 20)

    ErrStep = 3
    UnSafeCode = 1 / (Arg - 30)

    ErrStep = 0
End Function

VBScript Error Handling: VBScript On Error, On Error GoTo 0, On , Error Handling is a very useful mechanism of programming languages like VBScript in order to deal with the errors and to continue the execution� The primary difference is that VBScript does not support the concept of error handling by continuing execution at a label. In other words, you cannot use On Error GoTo in VBScript. Instead, use On Error Resume Next and then check both Err.Number and the Count property of the Errors collection, as shown in the following example:

You can regroup your steps functions calls in a facade function :

sub facade()
    call step1()
    call step2()
    call step3()
    call step4()
    call step5()
end sub

Then, let your error handling be in an upper function that calls the facade :

sub main()
    On error resume next

    call facade()

    If Err.Number <> 0 Then
        ' MsgBox or whatever. You may want to display or log your error there
        msgbox Err.Description
        Err.Clear
    End If

    On Error Goto 0
end sub

Now, let's suppose step3() raises an error. Since facade() doesn't handle errors (there is no On error resume next in facade()), the error will be returned to main() and step4() and step5() won't be executed.

Your error handling is now refactored in 1 code block

Handling errors in VBScript, The primary difference is that VBScript does not support the concept of error handling by continuing execution at a label. In other words, you� This is where the second element of VBScript’s error handling comes in. VBScript includes an error object, named Err, which, when used in conjunction with On Error Resume Next, adds much more functionality to error handling, allowing you to build robust programs and relatively sophisticated error handling routines.

I'm exceptionally new to VBScript, so this may not be considered best practice or there may be a reason it shouldn't be done this that way I'm not yet aware of, but this is the solution I came up with to trim down the amount of error logging code in my main code block.

Dim oConn, connStr
Set oConn = Server.CreateObject("ADODB.Connection")
connStr = "Provider=SQLOLEDB;Server=XX;UID=XX;PWD=XX;Databse=XX"

ON ERROR RESUME NEXT

oConn.Open connStr
If err.Number <> 0 Then : showError() : End If


Sub ShowError()

    'You could write the error details to the console...
    errDetail = "<script>" & _
    "console.log('Description: " & err.Description & "');" & _
    "console.log('Error number: " & err.Number & "');" & _
    "console.log('Error source: " & err.Source & "');" & _
    "</script>"

    Response.Write(errDetail)       

    '...you could display the error info directly in the page...
    Response.Write("Error Description: " & err.Description)
    Response.Write("Error Source: " & err.Source)
    Response.Write("Error Number: " & err.Number)

    '...or you could execute additional code when an error is thrown...
    'Insert error handling code here

    err.clear
End Sub

VBScript Err Objects with VBScript, By default, if an error occurs, VBScript terminates script execution and RhinoScript reports the error back to the user. Sometimes this default error processing is not desirable. In this case, the Err object and the On Error statement can be used to let scripts perform their own error handling. The "On Error Goto [label]" syntax is supported by Visual Basic and Visual Basic for Applications (VBA), but VBScript doesn't support this language feature so you have to use On Error Resume Next as described above.

On Error - VBScript, Error handling in VBScript is very basic, there is no option to immediately branch to an error handling routine. VBScript does not support GoTo or labels that would � Situations like issues in mathematical computations or any type of errors can be handled with the help of Error Handling. Now, Let’s see some of the methods of Error Handling in the VBScript. Methods of Error Handling in the VBScript. VBScript basically supports 2 main methods to handle errors in the scripts. They are as follows:

VBScript - Error Handling, VBScript - Error Handling - There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors. The "On Error Goto [label]" syntax is supported by Visual Basic and Visual Basic for Applications (VBA), but VBScript doesn't support this language feature so you have to use On Error Resume Next as described above.

Handling Errors in Script Extensions, In VBScript, you can handle errors using the On Error Resume Next statement in The try block contains code that may raise an error, the catch block -- code� This is where the second element of VBScript’s error handling comes in. VBScript includes an error object, named Err, which, when used in conjunction with On Error Resume Next, adds much more functionality to error handling, allowing you to build robust programs and relatively sophisticated error-handling routines.

Comments
  • Dylan's response is about as good as VB gets in the Error handling department. This is why I always used Javascript when I could get away with it.
  • You could change WScript.Echo within the If statement to call a Function or Sub, which could in turn exit the application, log the error, etc.
  • "contains the reuslts of the last operation performed". Is that true? Seems like it gets the last error which is a big difference.
  • Despite MS's documentation suggesting that err.clear needs to be used after each check of the object, to prevent previous errors tripping the next check (e.g., technet.microsoft.com/en-us/library/ee692852.aspx), in my experience err is cleared "by itself" as the script progresses. Without testing further, my guess is utilising objects clears err as a by product of their internal operations.
  • @user66001 Agreed but still safer to explicitly call Err.Clear.
  • Not the clearest example I've ever seen but I get the concept.
  • @Lankymart would you mind linking a clearer example that you've seen then, or instead suggest how omegastripes can improve this example?
  • For a second, I got the impression I missed a new software engineering paradigm called "omegastripes" lol
  • This is ASP Classic, not plain old VBScript