*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:
Your program can intercept and attempt to process errors instead of allowing control to pass to_$ (setq catchit (vl-catch-all-apply '/ '(50 5)))
10
apply to perform the division.
The result from this example is the same as if you had usedvl-catch-all-apply is in catching errors and allowing your program to continue execution.
The value ofTo catch errors with vl-catch-all-apply
(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)
)
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.
This function accepts two number arguments and uses(catch-me-if-you-can 50 2)
The function should return 25.
(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) ->
y, catch-me-if-you-can indicates that it will continue processing.
If you entervl-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.
Try modifying this example by changingvl-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.
The