Catching Errors and Continuing Program Execution

AutoCAD AutoLISP & Visual LISP

 
Catching Errors and Continuing Program Execution
 
 
 

Your program can intercept and attempt to process errors instead of allowing control to pass to *error*. The vl-catch-all-apply function is designed to invoke any function, return a value from the function, and trap any error that may occur. The function requires two arguments: a symbol identifying a function or lambda expression, and a list of arguments to be passed to the called function. The following example uses vl-catch-all-apply to divide two numbers:

_$ (setq catchit (vl-catch-all-apply
'/ '(50 5)))
10

The result from this example is the same as if you had used apply to perform the division.

The value of vl-catch-all-apply is in catching errors and allowing your program to continue execution.

To catch errors with vl-catch-all-apply

  1. The following code defines a function named catch-me-if-you-can. Copy the code into a VLISP text editor window:
    (defun catch-me-if-you-can (dividend divisor / errobj)
      (setq errobj (vl-catch-all-apply '/ (list dividend divisor)))
      (if (vl-catch-all-error-p errobj)
        (progn
          (print (strcat "An error occurred: "
                         (vl-catch-all-error-message errobj)
                 )
          )
          (prompt "Do you want to continue? (Y/N) -> ")
          (setq ans (getstring))
          (if (equal (strcase ans) "Y")
            (print "Okay, I'll keep going")
          )
        )
        (print errobj)
      )
      (princ)
    )

    This function accepts two number arguments and uses vl-catch-all-apply to divide the first number by the second number. The vl-catch-all-error-p function determines whether the return value from vl-catch-all-apply is an error object. If the return value is an error object, catch-me-if-you-can invokes vl-catch-all-error-message to obtain the message from the error object.

  2. Load the function.
  3. Invoke the function with the following command:
    (catch-me-if-you-can
    50 2)

    The function should return 25.

  4. Intentionally cause an error condition by invoking the function with the following command:
    (catch-me-if-you-can
    50 0)

    The function should issue the following prompt in the AutoCAD Command window:

    "An error occurred: divide by zero" Do you want to continue? (Y/N) ->

    If you enter y, catch-me-if-you-can indicates that it will continue processing.

    Try modifying this example by changing vl-catch-all-apply to apply. Load and run the example with a divide by zero again. When apply results in an error, execution immediately halts and *error* is called, resulting in an error message.

    The vl-catch-* functions are especially important when you use ActiveX with AutoLISP. Many of the AutoCAD ActiveX automation methods are designed to be used in the “programming by exception” style. This means they either return useful values if they succeed, or raise an exception if they fail (instead of returning an error value). If your program uses ActiveX methods, you must prepare it to catch exceptions, otherwise the program halts, leaving the user at a Command prompt. See Handling Errors Returned by ActiveX Methods for an example using vl-catch-all-apply with ActiveX.