SoFunction
Updated on 2025-03-10

VBS tutorial: VBscript statement-On Error statement

On Error Statement

Enable or disable error handlers.

On Error Resume Next
On Error GoTo 0

illustrate

If not used in your codeOn Error Resume NextStatement, The runtime error that occurs will display an error message, and the execution of the code will also terminate. However, the specific operation is determined by the host running the code. The host can sometimes selectively handle various errors. In some cases, it can activate the script debugger where the error occurs. In other cases, since the host cannot notify the user, the error that occurs is not specified. As for how to handle errors, it depends entirely on the function of the host.

In any special procedure, the error that occurs is generally not fatal as long as the error handler is enabled on the call stack. If the local error handler is not enabled in a process, when an error occurs, control can be transferred through the stack call until a process with an error handler is found and the error is handled where the error occurs. If the error handler is not found during the call stack, the error message is displayed where the error occurs, and the code execution is terminated, or the error is handled correctly through the host.

On Error Resume NextWill cause the program to continue execution as the statement after the error statement, or as the process called the most recent process (this process containsOn Error Resume NextThe statement in the statement) continues to run. This statement can continue to execute the program regardless of runtime errors, and then you can establish an error handling routine inside the process. When calling another procedure,On Error Resume Next The statement becomes inactive. So, if you want to perform internal error handling in a routine, it should be executed in each called routineOn Error Resume NextStatement.

When another procedure is called, use is prohibitedOn Error Resume Next statements, so if you want to embed an error handler in your routine, you need to execute every time the routine is calledOn Error Resume NextStatement. When exiting a process, the error handler can restore to its state before entering the exited process.

If you have enabled the On Error Resume Next error handler, you can use On Error GoTo 0 to disable the error handler.

The following example shows how to use the On Error Resume Next statement:

On Error Resume  6  'Overflow error generated。MsgBox ("Error # " & CStr() & " " & )    'Clear error。