Here is my error handling bungle.
(defun *error* (msg /)
(template:on-error msg)
(princ)
)
I borrowed this following piece from one of Kerry's posts, I like the way he put it together.
;; Error control Thanks to Kerry - theSwamp
(pragma '((unprotect-assign template:on-error)))
;;
(defun template:on-error (msg / tmp)
;;----- Cancel any Active Commands -----------------------------
(while
(> (getvar "cmdactive") 0)
(command)
)
(setvar "menuecho" 1)
(command ".undo" "End" ".undo" "Back")
[color=green]; I've learned to only use the ".undo" command on lengthy routines where several things occur, and then with caution.[/color]
;;----- Display error message if applicable _-------------------
(cond ((not msg)) ; no error, do nothing
((member
(strcase msg t) ; cancel
'("console break" "function cancelled" "quit / exit abort")
)
(princ "\n\tFunction Cancelled\n\tFunction Cancelled")
)
((princ (strcat "\nApplication Error: "
(itoa (getvar "errno"))
" :- "
msg "\n"
)
)
)
)
;;----- Reset System Variables ----------------
(setvar "osmode" origOS)
(setvar "autosnap" origASN)
(setvar "menuecho" 0)
(setvar "cmdecho" 1)
(command ".redraw")
(setq *error* nil)
(princ)
)
;;
(pragma '((protect-assign template:on-error)))
;;
I've been slowly learning more and more about the functionality of error handling, and the more I learn, the more I learn there is so much more to learn than what I initially determined.
And I have recently discovered, as Gile pointed out:
Hi,
IMO there's no 'best error subroutine'.
The error subroutine may be different for each main routine according to what you may have to reset:
- ending undo mark (no need to test uFlag with vla-EndUndoMark)
- restoring sysvar values
- closing an opened file
- erasing a temporary entity
- etc.
And I am now wondering if it is worth having several small error handling cases in a large LiSP routine, each focusing on a particular issue, rather than one final error handling routine to cover any and all there is in the LiSP routine?.