On error resume next, on error goto 0, err usage

The VBScript language provides two statements and an object to handle “runtime errors,” as follows:

On Error Resume Next statement

On Error Goto 0 statement

Err object

A brief introduction to On Error Resume Next, On Error Goto 0, Err

The On Error Resume Next statement and On Error Goto 0 statements indicate what to do when “runtime Error” occurs.
When you add the On Error Resume Next statement, if the following program has a “runtime Error,” it will continue to run without interruption.
When the On Error Goto 0 statement is added, if the following program has a “runtime Error”, an “Error message” is displayed and execution of the program is stopped.
The Err object holds the error message

The following examples are used to explain On Error Resume Next, On Error Goto 0, and Err

The On Error Resume Next statement was not added

If the “On Error Resume Next” statement is not included, when “run time Error” occurs, an “Error message” is displayed and execution of the program is stopped.
Example (/test.asp) :

i = 1/0   Divide by '0', generate "runtime error", display "error message" and stop program execution.
Response.Write "After the divide is executed" 'This sentence will not be executed'
%>

Results:

Microsoft VBScript runtime error error ‘800A000b’
Be zero except
/ test. The asp, line 2

Add the On Error Resume Next statement

When we add the “On Error Resume Next” statement somewhere, subsequent programs don’t show “Error messages” and continue to run even if they do.
For example:

On Error Resume Next   The program that follows will continue to run even if a "runtime error" occurs.
i = 1/0 '0 to divide, this is a "runtime error", but because of the above On Error Resume Next, the execution will not be interrupted, but will continue to run.
Response.Write "after the divide is executed" 'This will be executed'
%>

Results:

After the division is performed

With the On Error Resume Next statement, use the Err object to get the Error message

After using On Error Resume Next, if there is an Error, the Err object will put the last Error message.
There are three important properties of Err object: Number, Source, and Description. They are error number, error source, and error description.
All you can catch are runtime errors, and If Err then is equivalent to If Err.Number then

Dim i
i = 1/0   'the fisrt wrong
undefined_function "test"   'the second wrong,Function undefined_function undefined
Response.Write Err.Description

Operation results:

Type mismatch

As you can see, it’s not being divided by zero

Use the On Error Goto 0 statement to let the system take over the handling of the Error

With the On Error Resume Next statement, the following programs will continue to run even if there is a “runtime Error.” But what if you want a later program to stop executing and display an error when it has a “runtime error”?
The answer is: use the On Error Goto 0 statement
Using the statement “On Error Goto 0”, the following program will prompt an Error and terminate the execution of the script as soon as an Error occurs.

Dim i
i = 1/0
Response.Write "After the execution of the first exclusion"
On Error Goto 0 'The statement after 'will alert for an error and end script execution as soon as an error occurs.
i = 1/0
Response.Write "After the execution of the second division"

Operation results:

After the first division is performed
Microsoft VBScript runtime error error ‘800A000b’
Be zero except
/ test. The asp, line 2

As you can see, the first response.write executes and prints the content, while the second response.write does not execute.

Talk about On Error Resume Next in detail

Scope of action for On Error Resume Next statement

The On Error Resume Next statement only applies to subsequent statements at this level. Does not apply to the function or subroutine being called, nor does it apply to the parent segment
The On Error Resume Next statement affects only this function if it appears in a function. It has no effect on either the “calling function” or the “called function”
If there is no On Error Resume Next statement in a subroutine, an Error in the subroutine will interrupt the subroutine and jump to the outer program that calls the subroutine. If the outer program contains the On Error Resume Next statement before the function call in question, it will then execute the statement after the function call. If the outer program does not have an On Error Resume Next statement before the function call that went wrong, it jumps to the outer program. This process is repeated until the environment containing the On Error Resume Next statement is found to continue running. If the outermost program also does not contain the On Error Resume Next statement, the default Error handler is used, which is to display the Error message and stop running.
For example:

 Dim i
 i = 1/0
 Response.Write "OK"
End Sub
Sub test1()
 test
 Response.Write "OK"
End Sub
On Error Resume Next
test1

Results:

After the division is performed

Neither OK is printed. Since On Error Resume Next is issued in the outermost layer, when something goes wrong with the subroutine being called, it jumps right out of the subroutine and into the outer code.
If you put an On Error Resume Next statement at the beginning of the subroutine, the runtime Error does not abort the subroutine.
For example, if you need to write a string to a file, you can access the file through a separate function to prevent errors from interrupting the entire program:

'returns True if it succeeds, or False on any error
Function WriteNewFile(strFileName, strContent)
  On Error Resume Next   'turn off the default error handler
  WiteNewFile = Flase   'default return value of function
  Set objFSO = CreateObject("scripting.FileSystemObject")
  If Err.Number = 0 Then Set objFile = objFSO.CreateTextFile(strFileName,True)
  If Err.Number = 0 Then objFile.WriteLine strContent
  If Err.Number = 0 Then objFile.Close
  If Err.Number = 0 Then WriteNewFile = True
End Function

The above program checks the Err object’s Number attribute before processing each program statement. If the value is 0 (no errors have occurred yet), then the creation and writing of the file can continue. If an error occurs, the script engine sets the value of the Err object’s property and proceeds to the next line.
The return value of the function is set to “True” as long as it works without causing an error. Otherwise, the function returns “False”.

On Error Goto 0 statement

In ASP 2.0 (although not documented) and ASP 3.0, the On Error Goto 0 statement restores the default Error handling behavior.
After running this statement, a run-time error results in default error handling, checking each nested program in the environment chain up to the home page code. If no other environment turns off the default error handling, the execution of the page will stop and the IIS default error page will be displayed.

Err object

In the previous example, after turning off default error handling, check to see if an error has occurred by checking the Number attribute of the Err object.
The Err object stores information about run-time errors
The following table shows the properties provided by the Err object.

</ th> </ th> </ tr>
Description </ td> sets or returns a string Description error </ td> </ tr> Number </ td> (default property) sets or returns a false value specified </ td> </ tr> Source </ td> Sets or returns the name of the object that generated the error

You can use these properties to check what kind of error occurred. For example, you can take different actions based on the error number, or you can provide the user with error information using the attribute values of Source and Description.

The following table shows the methods provided by the Err object.

</ th> </ th> </ tr>
Clear </ td> remove all current Err object set </ td> </ tr> Raise </ td> generate a runtime error </ td> </ tr> </ tbody> </ table>

A “custom error” is generated using the Err object.

You can generate a “custom error” using the Err object. Why do we do this?Because sometimes you want to send a custom error message to the user. You can set the Err object’s properties to whatever value you want, and then call the Raise method to cause such an error, which will stop the program and pass the error back down the call chain.
The following example shows how to handle errors when reading a text file on a server disk. Notice how to use the constant vbObjectError to make sure that the selected error number does not confuse an existing error number. By adding arbitrarily selected error Numbers to this constant, you can ensure that you are not confused with predefined errors.

  Set objFSO = CreateObject("scripting.FileSystemObject")
  Set objFile = objFSO.OpenTextFile("strFileName", ForReading)
  Select Case Err.Number
   Case 0   'OK, take no action
   Case 50,53   'standard file or path not found errors
    'create custom error values and raise error back up the call chain
    intErrNumber = vbObjectError + 1073     'custom error number
    strErrDescription = "The file has been deleted or moved. "
    strErrSource = " ReadThisFile function"
    Err.Raise intErrNumber, strErrSource, strErrDescription
    Exit Function
   Case Else   'som other error
    'raise the standard error back up the call chain
    Err.Raise Err.Number, Err.Source, Err.Description
    Exit Function
  End Select
  ReadThisFile = objFile.ReadAll   ' we opened it OK, so return the content
  objFile.Close
End Function

The code that calls this function can use the On Error Resume Next statement and catch the Error that this function produces.

strContent = ReadThisFile("myfile.txt")
If Err.Number = 0 Then
    Response.Write "File content is:<br/>" & strContent
Else
    Response.Write Err.Source & "<br/>" & Err.Description
End If

Read More: