Re: Error Handling in LISP/Net
It is not just T/nil it is (T or any value) / nil.
It is just the 'and' and 'or' expressions recognize nil as no and anything else as yes.
The error trap returns T or a value for success and nil for failure.
Error message can also displayed using this variation of the error trap function
Code:
(defun ErrorTrap (symFunction / objError result)
(if (vl-catch-all-error-p
(setq objError (vl-catch-all-apply
'(lambda (XYZ)(set XYZ (eval symFunction)))
(list 'result))))
(and
DEBUG
(princ
(strcat "\nError: "
(vl-catch-all-error-message objError)
". While evaluating the expression: "
(vl-princ-to-string symfunction) "\n"
)
)
nil
)
(or result
'T
)
)
)
Example of code
Code:
Command: (setq DEBUG 1)
Command: (errortrap '(/ 1 0))
Error: divide by zero. While evaluating the expression: (/ 1 0)
nil
Re: Error Handling in LISP/Net
I'd rather want control over the result. E.g. you might want to have a nil returned if the result is used to test if your code needs to continue down a specific path. So I'd add an argument to the errortrap function to indicate a default return (instead of nil / T) e.g.
Code:
(defun net-error (value)
(and (numberp value) (>= value 0) (<= value 0) (/= value 0)))
(defun error-trap (toEval default / result error debug)
(defun debug (DotNet)
(if *DEBUG*
(princ (strcat "\nError: "
(cond (DotNet *NET-ERROR-MESSAGE*)
((vl-catch-all-error-p error) (vl-catch-all-error-message error))
("Unknown error"))
". While evaluating the expression: "
(vl-princ-to-string toEval)
"\n")))
default)
(cond ((vl-catch-all-error-p
(setq error (vl-catch-all-apply
(function (lambda (output) (set output (eval toEval))))
(list 'result))))
(debug nil))
((net-error result) (debug t))
((null result) default)
(result)))
Then using it:
Code:
_$ (error-trap '(/ 1 0) T)
Error: divide by zero. While evaluating the expression: (/ 1 0)
T
_$ (error-trap '(test-NAN) T)
Error: Testing DotNet LispFunction error token. While evaluating the expression: (test-nan)
T
_$ (error-trap '(test-NAN) nil)
Error: Testing DotNet LispFunction error token. While evaluating the expression: (test-nan)
nil
_$ (error-trap '(test-NAN) 12345)
Error: Testing DotNet LispFunction error token. While evaluating the expression: (test-nan)
12345
Re: Error Handling in LISP/Net
OK, I can see some advantage with adding a default success return variable.
I have to think about whether I want to adopt it.
It would be good if that could be an optional argument.
I still kinda like the simplicity of nil.
Its all good.
Did you check out the new ADOX thread?
Re: Error Handling in LISP/Net
Quote:
Originally Posted by
peter
It would be good if that could be an optional argument.
Yes indeed. Though how to accomplish an optional argument for this in AutoLisp is beyond me. All I can think which might work is to redo that error trap routine in ObjectARX, since you cannot do it through DotNet (some of the arguments would cause errors, since DotNet does not allow stuff like ActiveX objects passed to/fro ALisp).