Author Topic: Do I really need error checking  (Read 1197 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
Do I really need error checking
« 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. )
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

mhupp

  • Bull Frog
  • Posts: 250
Re: Do I really need error checking
« Reply #1 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. )
« Last Edit: December 15, 2022, 11:10:25 PM by mhupp »

jlogan02

  • Bull Frog
  • Posts: 327
Re: Do I really need error checking
« Reply #2 on: December 14, 2022, 06:43:43 PM »
Good point. It can' t hurt.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2162
  • class keyThumper<T>:ILazy<T>
Re: Do I really need error checking
« Reply #3 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 )
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

jlogan02

  • Bull Frog
  • Posts: 327
Re: Do I really need error checking
« Reply #4 on: December 15, 2022, 05:20:35 PM »
Thanks. I'll double check.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

mhupp

  • Bull Frog
  • Posts: 250
Re: Do I really need error checking
« Reply #5 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. )
« Last Edit: December 16, 2022, 11:56:56 AM by mhupp »