Jump to content

Recommended Posts

Posted

I think all of us know what a pain it can be to run a lisp which either crashes or someone presses the Esc key too soon. Some real problems can result from this especially if you've reset environment variables. I found this code somewhere back in history and it's served me well. But I was wondering if others had routines which may improve on this method. I have this code in a file of it's own and it get's loaded as needed.

 

The best part of this is that if the program crashes or someone hits Esc, the error routines kick in and all variables which were set for the program to run are reset to their original values. Which makes for happy users and less complaints.

 

 

(defun c:main_program ()
; start the programs off like this
; INITIALIZE ------------------------------------------------------------------------------  
 (load "/my_path/ErrorTrap.lsp")
 (error)
 (initerr)
 (setvar "cmdecho" 0)
 (setvar "osmode" 0)
 (setvar "pickbox" 0)
 (command "._CLAYER" "0")
 (setvar 'CECOLOR "ByLayer")
 (setvar 'CELTYPE "ByLayer")

 .
 .
 .

;-------------------------------------------------------------------------------------;
; Program Name: ErrorTrap.lsp                          LATEST REVISIONS 2012-10-12    ;
; Date Created: 10-12-2012                                                            ;
; Company:      
;                                                                                     ;
;               Error Trapping Routines                                               ;
;                                                                                     ;
;-------------------------------------------------------------------------------------;
; Revision History                                                                    ;
; Rev  By     Date       Description                                                  ;
;-------------------------------------------------------------------------------------;
; 1    BT  10-12-2012   Initial version                                              ;
;-------------------------------------------------------------------------------------;

;-------------------------------------------------------------------------------------;
; ERROR TRAPPING ROUTINES                                                 10-12-2012  ;
;-------------------------------------------------------------------------------------;
(defun error()
(prompt "\nGlobal Error Trap Loaded")
(princ)
)
;*====================================================================;
(defun initerr ()
 (setq oldlayer (getvar "clayer"))
 (setq oldsnap (getvar "osmode"))
 (setq oldpick (getvar "pickbox"))
 (setq oldblip (getvar "blipmode"))
 (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" oldblip)
 (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" oldblip)
 (setvar "menuecho" 0)
 (setvar "highlight" 1)
 (setvar "osmode" oldsnap)
 (setvar "pickbox" oldpick)
 (setvar "CELTYPE" "ByLayer")
 (command "CELTSCALE" 1.00)
 (princ)
)
;*====================================================================;

Posted

I used to do something similar when I first started to incorporate error handling... I found though, that different functions needed different 'restore' functionality, and by defining my own error function, I either needed to change the name of each-and-every-one so as to not rick overwriting another, or simply embed the error handler as a local variable, so the default error handler is never accidentally overwritten, and is restored as soon as my local error handler goes out of scope.

 

The latter (localized error handler) has proved to simpler, easier to maintain, and all around better for my work.

 

HTH

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...