TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jlogan02 on December 13, 2022, 06:01:18 PM

Title: Do I really need error checking
Post by: jlogan02 on December 13, 2022, 06:01:18 PM
This routine doesn't have any user actions to it except selecting a button that launches the routine. Is there really any reason for error checking for routines that don't have user action required within the code?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:ST01ToBy (/ *error* msg col )
  2.  
  3.   (defun *error* (msg)
  4.    
  5.     (if (not (member msg '("Function cancelled" "quit / exit abort")))
  6.       (princ (strcat "\nError: " msg))
  7.     )
  8.     (princ)
  9.   )
  10.  
  11.   (setvar 'cmdecho 0)
  12.      
  13.   (if
  14.     (setq col (ssget "X" '((0 . "*Dimension,MultiLeader,Hatch,*Line,*Text,Arc,Circle") (62 . 1))))
  15.     (foreach x (mapcar 'cadr (ssnamex col))
  16.         (entmod (append (entget x) '((62 . 256)))))
  17.   )
  18.  
  19.    (command "_layer" "Make" "LINE1" "color" "yellow" "LINE1" "")
  20.  
  21.   (setvar 'cmdecho 1)
  22.  
  23.  (princ)
  24. )
Title: Re: Do I really need error checking
Post by: mhupp on December 14, 2022, 08:46:14 AM
Always a potential for an error. you can set variables back to what they were before the lisp was called, or undo changes. the code isn't run unless their is an error so whats the harm?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:ST01ToBy (/ *error* msg col)
  2.   (defun *error* (msg)
  3.     (if (not (member msg '( "Function cancelled" "quit / exit abort")))
  4.       (princ (strcat "\nError: " msg))
  5.     )
  6.     (vla-endundomark Drawing)
  7.     (command "_U") ;undo if error    
  8.     (setvar 'cmdecho 1) ;set cmdecho back to one if their is an ERROR
  9.     (princ)
  10.   )
  11.   (vla-startundomark Drawing)
  12.   (setvar 'cmdecho 0)
  13.   (if (setq col (ssget "X" '((0 . "*Dimension,MultiLeader,Hatch,*Line,*Text,Arc,Circle") (62 . 1))))
  14.     (foreach x (mapcar 'cadr (ssnamex col))
  15.       (entmod (append (entget x) '((62 . 256))))
  16.     )
  17.   )
  18.   ;(command "_layer" "Make" "LINE1" "color" "yellow" "LINE1" "")
  19.   (entmake '((0 . "LAYER") (100 . "AcDbSymbolTableRecord") (100 . "AcDbLayerTableRecord") (2 . "LINE1") (70 . 0) (62 . 2)))  
  20.   (setvar 'cmdecho 1)
  21.   (vla-endundomark Drawing)
  22.   (princ)
  23. )
Title: Re: Do I really need error checking
Post by: jlogan02 on December 14, 2022, 06:43:43 PM
Good point. It can' t hurt.
Title: Re: Do I really need error checking
Post by: kdub_nz on December 14, 2022, 07:01:35 PM
Just be careful where you place the 'undo'.

Haven't tested, but I think the 'undo' in your handler will override the setvar ( a couple lines earlier )
Title: Re: Do I really need error checking
Post by: jlogan02 on December 15, 2022, 05:20:35 PM
Thanks. I'll double check.
Title: Re: Do I really need error checking
Post by: mhupp on December 15, 2022, 11:56:54 PM
Thanks kdub code updated.

--Edit

Also ran into this the other day. In BricsCAD you can just run the ssget with out an if statement and it wouldn't error if nil. sent a lisp to someone running AutoCAD and it does error if nil.  It also had an error trap so nothing was coming up.
They kept typing the command a couple more times. until they asked me why it wasn't working.

so while not displaying the default error maybe display a coustom one like.

Code - Auto/Visual Lisp: [Select]
  1. (defun *error* (msg)
  2.   (if (not (member msg '( "Function cancelled" "quit / exit abort")))
  3.     (princ (strcat "\nError: " msg))
  4.     (princ "\nSomething Went Wrong Contact: Name Here")
  5.   )
  6.   (vla-endundomark Drawing)
  7.   (command "_U")       ;undo if error
  8.   (setvar 'cmdecho 1)  ;set cmdecho back to one if their is an ERROR
  9.   (princ)
  10. )