This is my error trap function that I have been presenting at my lectures at Autodesk University for the last couple years. It makes error trapping as painless as I could make it.
If you wrap "ANY" lisp expression with this function and a quote like
(errortrap (quote (setq X (/ 1.0 0.0))))
The errortrap function will return a nil for any error, the result of an expression it is evaluating if it returns a value, and a T if the expression it is evaluating is successful and returns a nil value.
For debugging, I have included the ability to have the global variable DEBUG set to anything other than nil to print out the error messages if I want to see them.
The trick is the vlax-catch-all-apply expression requires you to pass it a list to get it to work. I have seen several routines where others were passing the arguments of an expression to be evaluated as the list. In this routine I am utilizing the little used SET function (not setq) to pass the RESULT to the catch-all-apply expression as a list. That way I can pass the entire expression I am trying to evaluate to the function. The quote expression is required to encapsulte the expression to be evaluated.
Hope it helps.
Peter
Code:
; Standardized Error Trap
(defun ErrorTrap (symFunction / objError result)
(if (vl-catch-all-error-p
(setq objError (vl-catch-all-apply
'(lambda (X)(set X (eval symFunction)))
(list 'result))))
(progn
(if DEBUG
(princ
(strcat "\n"
(vl-princ-to-string (vl-catch-all-error-message objError))
"\nWhile evaluating the expression: "
(vl-princ-to-string symfunction)
"\n"
)
)
)
nil
)
(if result result 'T)
)
)