Error Handling in AutoLISP

AutoCAD AutoLISP & Visual LISP

 
Error Handling in AutoLISP
 
 
 

The AutoLISP language provides several functions for error handling. You can use these functions to do the following:

  • Provide information to users when an error occurs during the execution of a program.
  • Restore the AutoCAD environment to a known state.
  • Intercept errors and continue program execution.

The complete list of error-handling functions is in AutoLISP Function Synopsis under the heading Error-Handling Functions Each error-handling function is described in the AutoLISP Reference.

If your program contains more than one error in the same expression, you cannot depend on the order in which AutoLISP detects the errors. For example, the inters function requires several arguments, each of which must be either a 2D or 3D point list. A call to inters like the following:

(inters 'a)

is an error on two counts: too few arguments and invalid argument type. You will receive either of the following error messages:

; *** ERROR: too few arguments
; *** ERROR: bad argument type: 2D/3D point

Your program should be designed to handle either error.

Note also that in AutoCAD, AutoLISP evaluates all arguments before checking the argument types. In previous releases of AutoCAD, AutoLISP evaluated and checked the type of each argument sequentially. To see the difference, look at the following code examples:

(defun foo ()
  (print "Evaluating foo")
  '(1 2))
(defun bar ()
  (print "Evaluating bar")
  'b)
(defun baz ()
  (print "Evaluating baz")
  'c)

Observe how an expression using the inters function is evaluated in AutoCAD:

Command: (inters (foo) (bar) (baz))

"Evaluating foo"

"Evaluating bar"

"Evaluating baz"

; *** ERROR: too few arguments

Each argument was evaluated successfully before AutoLISP passed the results to inters and discovered that too few arguments were specified.

In AutoCAD Release 14 or earlier, the same expression evaluated as follows:

Command: (inters (foo) (bar) (baz))

"Evaluating foo"

"Evaluating bar" error: bad argument type

AutoLISP evaluated (foo), then passed the result to inters. Since the result was a valid 2D point list, AutoLISP proceeds to evaluate (bar), where it determines that the evaluated result is a string, an invalid argument type for inters.