There are three types of errors (VBScript / VBA) programming:
- Grammatical errors
- Runtime Error
- logical error
Grammatical errors
Syntax error (also known as parsing errors) occurred in the interpretation of time VBScript. For example, the following line causes a syntax error, because it lacks a closing parenthesis.
Function ErrorHanlding_Demo() dim x,y x = "Yiibai Yiibai" y = Ucase(x End Function
Runtime Error
Runtime error (also referred to as an exception) occurred during the execution, after the explanation.
For example, the following line will cause a runtime error, because the syntax is correct here, but at run time it is trying to call fnmultiply
, but this is a function that does not exist.
Function ErrorHanlding_Demo1() Dim x,y x = 10 y = 20 z = fnadd(x,y) a = fnmultiply(x,y) End Function Function fnadd(x,y) fnadd = x + y End Function
logical error
Logical errors may be the most difficult to trace of the wrong type. These errors are not errors of grammar or run-time results. In contrast, when you make a mistake in logic driven script, and did not get the expected results, this happens.
You may not be able to catch these errors, because it depends on the business needs and what type of logic to join in the program.
For example, a digital divide by zero, or write a script to enter an infinite loop.
Error object
Suppose we have a run-time error, to stop the execution by displaying an error message. As a developer, if you want to catch the error, then use the Error
object.
example
In the following example, Err.Number
given the error number, Err.Description
error is given below.
Err.Raise 6 ' Raise an overflow error. MsgBox "Error # " & CStr(Err.Number) & " " & Err.Description Err.Clear ' Clear the error.
Error Handling
VBA enable error-handling routines, it can also be used to disable an error handling routine. No On Error
statement, any run-time error occurs is fatal: error message is displayed, and the implementation of a sudden stop.
On Error { GoTo [ line | 0 | -1 ] | Resume Next }
Numbering | Keyword | description |
---|---|---|
1 | GoTo line | Enable error handling routine specified in the required line argument in the row began. The line must specify the On Error statement in the same process, otherwise a compile-time error occurs. |
2 | GoTo 0 | Disable error handler in the current procedure enabled and reset Nothing . |
3 | GoTo -1 | Disable the current process to enable the exception and reset it to Nothing . |
4 | Resume Next | When you specify runtime error occurs, control is transferred to the statement false statement occurred after, and continue from that point. |
example
Public Sub OnErrorDemo() On Error GoTo ErrorHandler ' Enable error-handling routine. Dim x, y, z As Integer x = 50 y = 0 z = x / y ' Divide by ZERO Error Raises ErrorHandler: ' Error-handling routine. Select Case Err.Number ' Evaluate error number. Case 10 ' Divide by zero error MsgBox ("You attempted to divide by zero!") Case Else MsgBox "UNKNOWN ERROR - Error# " & Err.Number & " : " & Err.Description End Select Resume Next End Sub