When a run-time error occurs in a Microsoft Visual Basic for Applications macro, an error message appears on the screen, and the macro either halts or behaves unpredictably.
To prevent the application from crashing or behaving unpredictably, you can include macro code that intercepts the error and tells the macro how to handle it. The process of intercepting and handling a run-time error is called "error trapping." The set of instructions that tells the application how to handle the error is called the "error-handling routine" or "error handler."
On Error Statement
The On Error statement causes Visual Basic for Applications to start or stop error trapping. The On Error statement also specifies a set of statements to execute if an error is encountered.
Err Function
The Err function returns the number of the error encountered.
Example Using the Err Function: Msgbox "The most recent error number is " & Err & _
". Its message text is: " & Error(Err)
You can also use the Error statement to simulate run-time errors. This is especially useful when you are testing your applications, or when you want to treat a particular condition as being equivalent to a run- time error. Any Visual Basic for Applications run-time error can be simulated by supplying the error code for the error in an Error statement. You can also use the Error statement to create your own user-defined errors by supplying an error code that does not correspond to a Visual Basic for Applications run-time error. The table containing a list of built-in errors appears earlier in this article (under the "Err Function" section). At this time, Visual Basic for Applications does not use all of the available numbers for built-in errors. In future releases of Visual Basic for Applications, the internal numbers will increase as more built- in errors are added. It is recommended that you start your error numbers at 50,000 and work your way up to 65,535 to avoid possible conflicts in the future.
Example Using Error Statement to Simulate Run-time Errors: Sub Test()
On Error Resume Next
Error 50000 'set the value of Err to 50000
If Err = 50000 Then
MsgBox "my own error occurred"
End If
End Sub
2006-07-23 23:23:16
·
answer #1
·
answered by Anonymous
·
0⤊
0⤋
Make sure you understand the difference between "error prevention" and "error recovery" procedures.
Type "error trapping 101" (witout the quotes) in a search engine to get your different procedures of error trapping for different programmming environment, etc.
2006-07-23 21:49:10
·
answer #2
·
answered by dranagar 5
·
0⤊
0⤋
Every language has (a) different way(s) of trapping errors. Check the manual.
2006-07-23 21:38:29
·
answer #3
·
answered by vinny_the_hack 5
·
0⤊
0⤋
Usually there is a return code or status to be checked after any File commands, Open, Read, Write etc.
OO languages allow you to Try and Catch any Exceptions. The File "commands" will tell you which Exceptions they will generate.
So. What language are you writing in?
2006-07-23 21:57:49
·
answer #4
·
answered by AnalProgrammer 7
·
0⤊
0⤋