Joanne said:
I've gone from being a total newbie, to an OK beginner programmer but
error
handling has got me flummoxed. Where do I put an "On Error Resume Next"
statement? Just one in the whole module? And how exactly does it work?
I
understand that it's supposed to skip the offending line of code and I
assume
the code keeps executing but, oh I don't know - just a general explanation
of
error handling would be greatly appreciated if someone had the time.
Hi Joanne
If you want error handling, it must go in each routine where you want it.
The simplest form of error handling is On Error Resume Next.
What this does is ignore the line of code with the error, and resume
execution from the next line of code.
The other form of error handling within VBA is On Error Goto <label> The way
this works is as follows. You have the On Error Goto command in your
routine, usually (but not necessarily) at the start. When a line triggers an
error, execution jumps to the label. A very simple example of a routine with
this kind of error is as follows
Sub ShowCustomProp()
On Error Goto Errhandle
MsgBox ActiveDocument.CustomDocumentProperties("Test property")
Exit Sub
Errhandle:
Err.Clear
MsgBox "The 'Test property' custom property does not exist"
Resume Next
End Sub
What this does is display the value of the "Test property" custom document
property. If the property doesn't exist, an error is thrown, and execution
jumps to the Errhandle label. The error is cleared, a message box displayed
telling the user what has happened, and execution resumed from the line
below where the error occured.
The Exit Sub command ensures that the normal excution path of the macro
doesn't drop into the error handler.
There are a number of approaches to error handling, including the following
1. Write your code such that errors cannot occur.
2. Write routine-specific error handlers for the special cases where you
anticipate errors can occur
3. Use On Error Resume Next
4. Have a general-purpose error handler that you apply to every routine, and
tweak it for special cases as necessary.
There are almost as many opinions on good error handling as there are
experienced programmers, so don't regard my opion (or any other for that
matter) as being authorititave, but I will provide my opinion anyway.
In many short routines, option #1 is a perfectly valid way of programming.
For instance, if you are toggling the Bold status of the selection, then
that is such a short routine that an error handler is really not necessary.
Option #2 is a valid approach in cases such as the code sample I gave, where
you know what sort of errors might occur, and you write specific exception
handling routines for them.
Option #3 is also perfectly adequate in many cases. The code sample above
could equally well have been written as follows
Sub ShowCustomProp()
Dim strProp as String
On Error Resume Next
strProp = CStr(ActiveDocument.CustomDocumentProperties("Test property"))
If Len(strProp) > 0 Then
MsgBox strProp
Else
MsgBox "The 'Test property' custom property does not exist"
End If
End Sub
Option #4 is in my opinion unnecessary for most projects. If you are
developing a project, and you hit an unexpected error, then about the most
helpful thing that can happen is what happens by default - execution stops
and the error code and description is displayed. For many routines, adding
an error handler is only useful if you are getting paid for each line of
code you write irrespective of whether it actually does anything. If an
error handler is actually required for a particular routine, then it is
probably required to do something quite specific, in which case you are back
into the territory of option #2.
Remember that error handling is not primarily for the purpose of debugging,
it is for handing cases where you deliberately allow individual lines of
code to run which will trigger an error in certain circumstances. In other
words, error handling is a program flow control mechanism just as valid as
If-Then or Do-Loop (though generally used less often than them). As such is,
it misleadingly named. I prefer to describe it as "exception handling". But
since Error is the keyword used in VBA, I'm always going to be fighting a
losing battle on that front!