(*error* string)
Display an error message.
This function allows you to define an error within your LISP routine. Errors can be caused by the programmer, or by user input. You need to be able to either test for these errors, or "trap" them when they occur.
Trapping Errors
Trapping an error refers to the process of looking at the error message and attempting to give the user feedback on why the error occurred, and what it means. The *error* function allows you to trap the errors that have occurred, and gives you an opportunity to handle them, and exit your program gracefully.
You may have only one *error* function in your program. However, because one function can be assigned to another with the setq function, you can have as many error handling functions in your program as you desire. Then to activate the error function you want, you set the *error* function to your error trap routine. Because there is only one active *error* function, you need to be kind to other programs or global settings. Before changing the *error* function, you should save the current error trap function in a temporary variable so that you may re-assign that function when you are done processing.
Defining the *error* Function
The definition of the *error* function is very specific. The function must be called *error* (or be set to *error* with a setq), and it must have one argument passing variable. You can name this variable whatever you like.
Examples
The following is an example of a simple error handling function:
(defun *error* (msg)
(princ (strcat "\nAn error has occurred in the program: " msg)
)
In the above example, when the error occurred, the error message is passed to the *error* function in the msg variable. The function then prints, "An error has occurred in the program:" followed by the error message.
Here is a more in-depth example of an error handling function in a real scenario. Notice that first we save the current *error* function in temperror before pointing it to our error handler routine. Then at the end of the myslope program, we return the *error* function to the previously saved function in temperror. It is important to notice that the last line of the myerror function also returns the *error* function to one we previously saved in temperror.
Code | Comments |
---|---|
(defun c:myslope ( ) | Defining the program "myslope". |
(setq temperror *error*) | Saving current error function to variable. |
(setq *error* myerror) | Setting *error* to the error handling routine. |
(setq p1 (getpoint "Select 1st point: ")) | Setting p1 to the user specified point. |
(setq p2 (getpoint p1 "Select 2nd point: ")) | Setting p2 to the user specified point. |
(setq slope (/ (-(cadr p2) (cadr p1)) | Setting slope to the slope formula using p1 and p2. |
(-(car p2) (car p1)))) | |
(princ (strcat "Slope equals " | Printing what slope equals to two decimal places onto the screen. |
(rtos slope 2 2))) | |
(setq *error* temperror) | Returning the error function to the variable. |
) |
The error handler:
Code | Comments |
---|---|
(defun myerror (msg) | Define the error handler "myerror". |
(if (= msg "divide by zero") | If the passing variable msg is the same as "divide by zero", |
(prompt "\n The line is vertical.") | then show this prompt, |
(prompt (strcat "\n" msg) | else show this prompt |
) | |
(setq *error* temperror) | Return the error function to the variable. |
) |