Author Topic: Error handler help needed  (Read 5359 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Error handler help needed
« Reply #15 on: August 10, 2006, 01:04:09 PM »
'kay,managed about 2 minutes on this so bear with me. Consider the implications of this little demo (no time to spell it out) --
Funny, it took less than 2 minutes for this to make my head hurt... :| I'll have to look this over to decipher it when I don't have real work to accomplish.....thanks Michael, it will give me something to do while my wife is driving on our trip to Oregon tomorrow.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Error handler help needed
« Reply #16 on: August 11, 2006, 01:04:43 PM »
Quick bash, may have errors ...

Code: [Select]
(defun c:Demo ( / _SetVars _Initializer _Terminator *error* _Main )

    (defun _SetVars ( lst )
        (mapcar
           '(lambda ( name value )
                (list
                    name
                    (getvar name)
                    value
                    (null
                        (vl-catch-all-error-p
                            (vl-catch-all-apply
                               '(lambda ( ) (setvar name value))
                            )
                        )
                    )
                )   
            )
            (mapcar 'car lst)
            (mapcar 'cadr lst)
        )
    )

    (defun _Initializer ( document / undoIsEnabled oldSysVars )
        ;;  variable *debug* is a lexical global
        (if *debug* (princ "Entering Initializer ... "))
        (cond
            (   (setq undoIsEnabled (< 0 (getvar "undoctl")))
                (while (eq 8 (logand 8 (getvar "undoctl")))
                    (vla-endundomark document)
                )
                (vla-startundomark document)
            )   
        )
        (setq oldSysVars
            (_SetVars
               '(   
                    ("cmdecho"   0)
                    ("regenmode" 1)
                    ("attreq"    0)
                    ;;  yada
                )
            )
        )
        (cond
            (   *debug*
                (if undoIsEnabled (princ "<Undo Started> ... "))
                (princ "Exiting Initializer\n")
            )
        )   
        ;;  return to caller
        (list
            (list 'document document)
            (list 'undoIsEnabled undoIsEnabled)
            (list 'oldSysVars oldSysVars)
        )
    )
   
    (defun _Terminator ( data / document undoIsEnabled oldSysVars )
        ;;  variable *debug* is a lexical global
        (if *debug* (princ "Entering Terminator ... "))
        (foreach arg '(document undoIsEnabled oldSysVars)
            (set arg (cadr (assoc arg data)))
        )
        (if (and undoIsEnabled document) (vla-endundomark document))
        (if oldSysVars (_SetVars oldSysVars))
        (cond
            (   *debug*
                (if (and undoIsEnabled document) (princ "<Undo Ended> ... "))
                (princ "Exiting Terminator\n")
            )   
        )
        (princ)
    )
   
    (defun *error* ( msg )
        (princ "*Abnormal program termination*\n")
        (if *debug* (vl-bt))
        ;;  variable terminatorData is a lexical global 
        (_Terminator terminatorData)
    )
   
    (defun _Main ( / terminatorData *debug* )
        (initget "Yes No")
        (if (eq "Yes" (getkword "Enable debug <verbose> mode? <No>: "))
            (setq *debug* t)
        )
        (setq terminatorData
            (_Initializer
                (vla-get-activedocument
                    (vlax-get-acad-object)
                )
            )
        )
        (initget "Yes No")
        (if (/= "No" (getkword "Do you want to crash me? <Yes>: "))
            (/ 1 0) ;; divide by zero error
        )
        (princ "*Normal program termination*\n")
        (_Terminator terminatorData)
    )
   
    (_Main)
   
)
   

DEMO [Enter]
Enable debug <verbose> mode? <No>: No
Do you want to crash me? <Yes>: No
*Normal program termination*

DEMO [Enter]
Enable debug <verbose> mode? <No>: No
Do you want to crash me? <Yes>: Yes
*Abnormal program termination*

DEMO [Enter]
Enable debug <verbose> mode? <No>: Yes
Entering Initializer ... <Undo Started> ... Exiting Initializer
Do you want to crash me? <Yes>: No
*Normal program termination*
Entering Terminator ... <Undo Ended> ... Exiting Terminator

DEMO [Enter]
Enable debug <verbose> mode? <No>: Yes
Entering Initializer ... <Undo Started> ... Exiting Initializer
Do you want to crash me? <Yes>: Yes
*Abnormal program termination*
Backtrace:
[0.52] (VL-BT)
[1.48] (*ERROR* "divide by zero")
[2.43] (_call-err-hook #<SUBR @0cfd7654 *ERROR*> "divide by zero")
[3.37] (sys-error "divide by zero") :ERROR-BREAK.32 nil
[4.29] (/ 1 0)
[5.23] (_MAIN)
[6.19] (C:DEMO)
[7.15] (#<SUBR @0cfd7dac -rts_top->)
[8.12] (#<SUBR @0d0f2334 veval-str-body> "(C:DEMO)" T #<FILE internal>) ...

Entering Terminator ... <Undo Ended> ... Exiting Terminator
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Error handler help needed
« Reply #17 on: August 11, 2006, 03:00:31 PM »
I've changed my mind about the setvars function, though I'll leave existing posts as is.

I think error trapping the individual setvar calls is a bad idea. I started to think about that approach as applies to my "real world" programming and I could not think of a single instance where I would want to mask a setvar error, that is, prevent a program from crashing. The simple reason being (for me) that a failed setvar call represents a design flaw in my program (either the variable name is wrong or the candidate value is invalid) so I'd actually want it to trash the program (so I could code it proper). As a result I would alter the setvars function to act accordingly --

Code: [Select]
(defun setvars ( lst )
    (mapcar
       '(lambda ( var val ) (list var (getvar var) (setvar var val)))
        (mapcar 'car lst)
        (mapcar 'cadr lst)
    )
)

Argument lst is (still) of the form '(("var" val)("var" val)), for example '(("cmdecho" 0)("regenmode" 1)).

The result is of the form form '(("var" oldval newval), for example '(("cmdecho" 1 0)("regenmode" 0 1)) so it can still be passed to the setvars function later, say at the end of program execution.

I realize this thread has become a ghost town <not sure how I killed it, tmi?> and I'm talking to myself but that's ok, point is still valid.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Error handler help needed
« Reply #18 on: August 11, 2006, 03:07:22 PM »
I realize this thread has become a ghost town <not sure how I killed it, tmi?> and I'm talking to myself but that's ok, point is still valid.
I'm still reading it.  I have just devoted more time to 'try' and learn C#, so I'm not posting as much when I see others have taken, or are going to take, care of someone.  I was just watching to see where you were going, and I was hoping it was here, as that is what I think is the best way.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Error handler help needed
« Reply #19 on: August 12, 2006, 11:17:18 AM »
I realize this thread has become a ghost town <not sure how I killed it, tmi?> and I'm talking to myself but that's ok, point is still valid.

I'm still reading it.  I have just devoted more time to 'try' and learn C#, so I'm not posting as much when I see others have taken, or are going to take, care of someone.  I was just watching to see where you were going, and I was hoping it was here, as that is what I think is the best way.

Thanks Tim, appreciated (on both counts).

PS: TMI meant "too much info", not a typo for Tim (in the remote chance that's the way you interpretted it).

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst