AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN


*Error* Trap Routine

Once you have written your AutoLisp routine you will find a need for an error trap routine. There is nothing worse than your program crashing or just being cancelled and finding that some of your system variables have been modified and no longer work as they should.
The following routine basically takes your settings and stores them.
If the routine runs correctly it simply resets these settings.
(This also saves you having to duplicate the same code over and over just to reset your system variables.)
If your program does crash or is cancelled it restores your settings back to where they were before the routine started.
The following is an example routine with the error trap incorporated.
(Hint : You could also make the error trap routine a stand alone program, load it from your Acad Lisp file and call it from multiple programs.
This way it would act as a Global Error Routine.)

(defun c:lsc ( / a1 a2)
  (initerr)	
  (setq a1 (getvar "ltscale"))
  (setq a1 (rtos a1))
  (setq a2 
      (strcat "THE LTSCALE OF THIS DRAWING IS " a1))
(alert a2)
(reset) 
(princ)
)
;;;*==========================================================
(defun initerr ()
  (setq oldlayer (getvar "clayer"))
  (setq oldsnap (getvar "osmode"))
  (setq oldpick (getvar "pickbox"))
  (setq temperr *error*)
  (setq *error* trap)
  (princ)
)
;;;*===========================================================
(defun trap (errmsg)
  (command nil nil nil)
  (if (not
	(member errmsg
	 '("console break" "Function Cancelled"))
      )
    (princ (strcat "\nError: " errmsg))
  )                 
  (setvar "clayer" oldlayer)
  (setvar "blipmode" 1)
  (setvar "menuecho" 0)
  (setvar "highlight" 1)
  (setvar "osmode" oldsnap)
  (setvar "pickbox" oldpick)
  (princ "\nError Resetting Enviroment ")
  (terpri)
  (setq *error* temperr)
  (princ)
)	
;;;*===========================================================
(defun reset ()
  (setq *error* temperr)
  (setvar "clayer" oldlayer)
  (setvar "blipmode" 1)
  (setvar "menuecho" 0)
  (setvar "highlight" 1)
  (setvar "osmode" oldsnap)
  (setvar "pickbox" oldpick)
  (princ)
)
;;;*======================================================
(princ)
 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website