There are really 2 subjects here that are very similar but are distinct - error trapping and error handling.
Error trapping is more of an instance specific type of thing and the others here have been doing a good job of giving examples of how to do that. Therefore I won't go into that.
Typically Error Handling is a more global or broad reaching method for handling errors. Error handling usually refers to the way a program/function deals with a fatal error (an error which crashes your application/program/function).
AutoCAD/AutoLISP provides an Error Handler that can be changed to suit your own needs. This function is the *error* function. A programmer can overwrite this function to suit there own needs. It takes a single argument. That argument is the message (string) that AutoCAD sends the function as the result of the fatal stoppage. There are lots of different messages that this error handler reads, but here are just a few:
"Function cancelled"
"quit / exit abort"
"bad argument type:..."
"too few agruments"
The beauty of this is that with a custom error handler a programmer can give their function an even more integrated look and feel. For instance, with using the AutoCAD standard *error* function, pressing the ESC key will cause the application to cease any processing and immediately dumping the user to the command line. What typically happens is that once this happens, any environment variables that the program was supposed only temporarily set, are not reset. That is where customizing the *error* function comes in. For instance the brief code below allows the user to crash out (press the ESC key) and have is settings still reset.
(defun c:some_application (/ *error*)
(defun *error* (msg)
(if
(not
(member
(strcase msg T)
'("quit / exit abort" "function cancelled" "console break")
)
)
(princ "\nError: " msg)
)
...reset any environment variables or close out any file objects or terminate any dialogs here...
(princ)
)
... the rest of your app goes here ...
)
Notice that the *error* function is nested within the parent and that it is localized within the parent. The reason that this is done is that this way the original *error* function is not lost after your function executes.
Something else you should pay attention is the IF NOT statment. What this is going to do is allow the user to ESC out of the function quietly. Meaning without an error message or leaving anything undone.
This is a good practice to employ in your code. It is as important as making your apps look and feel like standard AutoCAD. Not only that it really does become a necessity when your programs start to get into the thousands of lines and the complexity becomes higher and higher.
I hope this makes sense to at least some of you out there in the electronic world. If not, then let me know, and I will attempt to clarify this mumbo jumbo. *sssssiiiiipppp* ...ahh... need more coffee now...