Err object in VBS Foundation

Time:2021-11-11

The err object is an internal object with a global scope that contains all the information about the error.

On error resume next ignores all errors generated at runtime

On error goto 0 cancel ignore error action

The main methods are: clear and raise
The main attributes are: description, HelpContext, helpfile, number and source

For detailed description of its properties and methods, see the following example description:

Method of err object

Clear

Description: clear all the current properties of err object, that is, clear error
Syntax: err.clear
Example:

?
1
2
3
4
5
On Error Resume next 'ignore all errors generated at runtime
MsgBox 5/0
MsgBox Err.Number 'output wrong value
Err.Clear 'clear all error messages
MsgBox Err.Number 'output 0

Raise

Description: defines a runtime error
Syntax: err. Raise (number, source, description, helpfile, helpcontent)
Parameter: Number: used to mark the error number
The source tag generates the wrong object or application name
Description: description information about the error
Helpfile: a valid path to the help file
Helpcontent: the topic of the help file
Example:

?
1
2
3
4
5
6
7
8
On Error Resume Next
Err.Raise 22,Vbs script,"Overflow","c:\test.txt" 'define a runtime error
MsgBox Error: & vbCrLf _
    & "Number:" & Err.Number & vbCrLf _
    & "Source:" & Err.Source & vbCrLf _
    & "Description:" & Err.Description & vbCrLf _
    & "Helpfile:" & Err.HelpFile
Err.Clear 'clear errors

Properties of err objects

  Description

Description: returns or sets the description of the error
Syntax: description (conid)
Example:

?
1
2
Desc = Err.Description 'return the description of error
Err.Description = "Type mismatch" 'set the description of error

HelpContext

Description: returns or sets the topic of the specified help information
Syntax: HelpContext (string)

Example:

?
1
2
HelpContext = Err. HelpContext 'return to the help topic for error
Err. HelpContext = "Type mismatch" 'set help topic for error

HelpFile

Description: returns or sets the address of the help file
Syntax: helpfile (filepath)
Example:

?
1
2
HelpFile = Err. HelpFile 'return to the address of the helpfile
Err. HelpFile = "c:\test.txt" 'set the address of the helpfile

Number

Description: returns or sets a numeric value indicating an error
Syntax: number (errid)
Example:

?
1
2
Number = Err. Number 'return the ID of error
Err. Number = "c:\test.txt" 'set the ID of error

Source

Description: returns or sets the object (or application name) that reports the error
Syntax: source (string)
Example:

?
1
2
Source = Err. Source 'return the object or application name of error
Err. Source = "box" 'set the object or application name of error