In VB and VBA, runtime errors are trapped using the On Error statement. This statement literally sets a trap for the system. When an error occurs, this statement automatically detours processing to your specially written error handler. The default error handling for the system is bypassed.
The On Error statement has three forms:
- On Error Resume Next
- On Error GoTo Label
- On Error GoTo 0
The On Error Resume Next statement is used when you want to ignore errors. This statement traps the error and instead of displaying an error message and terminating the program, it simply moves on to the next line of code and continues processing. For example, if you wanted to create a subroutine to iterate through model space and change the color of each entity, you know that AutoCAD will throw an error if you try to color an entity on a locked layer. Instead of terminating the program, simply skip the entity on the locked layer and continue processing the remaining entities. The On Error Resume Next statement lets you do just that.
The On Error GoTo Label statement is used when you want to write an explicit error handler. This statement traps the error and instead of displaying an error message and terminating the program, it jumps to a specific location in your code. Your code can then respond to the error in whatever manner is appropriate for your application. For example, you can expand the example above to display a message containing the handle for each entity on the locked layer.
Handle errors with the On Error Resume Next statement
The following subroutine iterates model space and changes the color of each entity to red. Try running this subroutine on a drawing with several entities, some of which are on a locked layer. Next, comment out the On Error Resume Next statement and run the subroutine again. You will notice the subroutine terminates at the first entity on the locked layer.
Sub Ch11_ColorEntities()
Dim entry As Object
On Error Resume Next
For Each entry In ThisDrawing.ModelSpace
entry.Color = acRed
Next entry
End Sub
Handle errors with the On Error GoTo statement
The following subroutine iterates model space and changes the color of each entity to red. For each entity on the locked layer, the error handler displays a custom error message and the handle of the entity. Try running this subroutine on a drawing with several entities, some of which are on a locked layer. Next, comment out the On Error GoTo MyErrorHandling statement and run the subroutine again. You will notice the subroutine terminates at the first entity on the locked layer.
Sub Ch11_ColorEntities2()
Dim entry As Object
On Error GoTo MyErrorHandler
For Each entry In ThisDrawing.ModelSpace
entry.Color = acRed
Next entry
' Important! Exit the subroutine before the error handler
Exit Sub
MyErrorHandler:
Msgbox entry.EntityName + " is on a locked layer." + _
" The handle is: " + entry.Handle
Resume Next
End Sub
The On Error GoTo 0 statement cancels the current error handler. The On Error Resume Next and On Error GoTo Label statements remain in effect until the subroutine ends, another error handler is declared, or the error handler is canceled with the On Error GoTo 0 statement.