Author Topic: Share your ERROR subroutine to get best one  (Read 10496 times)

0 Members and 2 Guests are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
Re: Share your ERROR subroutine to get best one
« Reply #30 on: July 07, 2010, 12:37:09 PM »
(setq olderr *error* *error* ErrorTrap)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Share your ERROR subroutine to get best one
« Reply #31 on: July 07, 2010, 01:25:48 PM »
> I tend not to use command statements in my code.

why? There is nothing wrong with COMMAND if you are prepared for it. Besides, two commands like that means <ESCAPE> <ESCAPE>

I find most command calls unreliable and of course there is the performance aspect to consider - so I only use them if I have no other alternative or unless the alternative is 100's of lines of code...

> ...the second 'princ' would not be evaluated as the previous expression returns True...

See what i mean about confusing? `aesthetics' be damed. [you] or someone needs to maintain this. Make it easier on yourself; ditch the fancy obscure tricks and focus on the elegance.

I don't think two lines within an OR is confusing - its practically an  (if (not <xxx>) (then <xxx>) ...), it was you that suggested including the second princ in the statement - at which point it may become a little obscure...

Why even check the string if your not going to print it if it passes the check BTW? I'm actually kinda confused.

Again, the OR acts like an (if (not <xxx>) (then <xxx>) ...) - I am checking the string so that I don't print the 'Function Cancelled' etc messages.

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
Re: Share your ERROR subroutine to get best one
« Reply #32 on: July 07, 2010, 02:02:15 PM »
TOOL - Ænima is a very good album to debug code to.



TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Share your ERROR subroutine to get best one
« Reply #33 on: July 08, 2010, 03:48:59 AM »

Lee,
John has an ongoing fixation about OR and COND .... you may ignore him if you like.    :lmao:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Share your ERROR subroutine to get best one
« Reply #34 on: July 08, 2010, 06:13:28 AM »

just a thought :

Don't we all stand on  the shoulders of giants ( a little ).
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Share your ERROR subroutine to get best one
« Reply #35 on: July 08, 2010, 06:25:31 AM »

just a thought :

Don't we all stand on  the shoulders of giants ( a little ).

YES we are.

CHulse

  • Swamp Rat
  • Posts: 504
Re: Share your ERROR subroutine to get best one
« Reply #36 on: July 08, 2010, 07:08:51 AM »

just a thought :

Don't we all stand on  the shoulders of giants ( a little ).

... or a lot! I know I'd be lost without the help of all of you here and at Cadtutor. I can atribute almost everything I know about cad to these forums ("self" taught)...
So Thanks.
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Share your ERROR subroutine to get best one
« Reply #37 on: July 08, 2010, 08:16:26 AM »

just a thought :

Don't we all stand on  the shoulders of giants ( a little ).

I suppose you hit the nail on the head - I too learn most of what I know from others examples  :-)

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Share your ERROR subroutine to get best one
« Reply #38 on: July 08, 2010, 08:18:00 AM »
TOOL - Ænima is a very good album to debug code to.





Thats a good album to do anything to.  It was the best live show I had ever seen.
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Share your ERROR subroutine to get best one
« Reply #39 on: July 08, 2010, 08:18:45 AM »
It was the best live show I had ever seen.
Agree. Both times.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
Re: Share your ERROR subroutine to get best one
« Reply #40 on: July 08, 2010, 09:36:31 AM »
Lee,
John has an ongoing fixation about OR and COND .... you may ignore him if you like.    :lmao:

Its not a `fixation'. *Under breath: I'll fix you.*

That is only because OR returns a Boolean value not...no, wait (let me just say it)...it is *not* the same as "(if (not (<blah>...".

IMHO if you want to use OR that way then you should just switch to Scheme otherwise do what the rest of us do; use IF and or COND.  However, i am not totally against the use of OR in some situations. I think OR can be a very elegant method but *I* know how to use it and i feel that most times the user just thinks its a fancy trick. Hence, my comment about debugging code. Ive hunted down these "boolean errors" and seen what happens when you dont "know your returns" can do to the flow of an app and i say good luck.


:P
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Share your ERROR subroutine to get best one
« Reply #41 on: July 09, 2010, 02:19:14 AM »
When these in different positions in the codes
Code: [Select]
[color=red]   (setq theERR *error*)
   (setq *error* ErrorHandling)[/color]
would the following codes make any difference?
Thanks for your helps.

Code: [Select]
(defun Test (/ *error* theERR)
   (defun ErrorHandling (msg)
    ...
   ); end of ErrorHandling
   ...
   MainFunctions Here
   ...
   [color=red](setq theERR *error*)
   (setq *error* ErrorHandling)[/color]
); end of Test

Code: [Select]
(defun Test (/ *error* theERR)
   (defun ErrorHandling (msg)
    ...
   ); end of ErrorHandling
   [color=red](setq theERR *error*)
   (setq *error* ErrorHandling)[/color]
   ...
   MainFunctions Here
   ...
); end of Test

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Share your ERROR subroutine to get best one
« Reply #42 on: July 09, 2010, 02:34:16 AM »
It's actually simpler than that.

You don't need the variable theERR  or the local-function  ErrorHandling

The *error* function is LOCAL to the routune ... so you don't need to be concerned with any global function named the same  (the acad default one) that may exist.


I'd do it like this :
Code: [Select]
(defun Test (/ *error* )
   (defun *error* (msg)
    ...
   ); end of ErrorHandling
   ...
   MainFunctions Here
   ...

  (princ)
); end of Test


for fun, have a look at the code I posted .. you'll see a statement that MAY cause a divide by zero error.
See if you can write a self contained routine that will trap that (or  in fact any) error.

Post the code when you're done. :)


[added]
BUT, if you wanted to use the methodology you posted, the second sample is correct.
The *error* function alias needs to be declared before it can be used ... so declare it after the handler definition
  
« Last Edit: July 09, 2010, 02:39:59 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Share your ERROR subroutine to get best one
« Reply #43 on: July 09, 2010, 03:05:02 AM »
Thanks.
What if the ErrorHandling function is outside of the Test code?

Sample 3
Code: [Select]
(defun ErrorHandling (msg)
    ...
); end of ErrorHandling

(defun Test (/ *error* theERR)
      ...
   MainFunctions Here
   ...
   [color=red](setq theERR *error*)
   (setq *error* ErrorHandling)[/color]
(princ)
); end of Sample 3

Sample 4
Code: [Select]
(defun ErrorHandling (msg)
    ...
); end of ErrorHandling

(defun Test (/ *error* theERR)
   [color=red](setq theERR *error*)
   (setq *error* ErrorHandling)[/color]
      ...
   MainFunctions Here
   ...
(princ)
); end of Sample 4

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Share your ERROR subroutine to get best one
« Reply #44 on: July 09, 2010, 03:14:32 AM »
Thanks.
What if the ErrorHandling function is outside of the Test code?

<.. >

In that case, simply do it like the following sample.

Just to clarify :
The AutoLisp engine looks for a function called *error* to handle breaks in evaluation.
If there is a Global function named *error* it will be used if no LOCAL *error* function is declared

Code: [Select]


(defun Test4a (/ *error* )
   (defun *error* (msg)
     ;; call the external handler
     (ErrorHandling msg)
     ;; do local handling
    ...
   )
   ;; ----- end of ErrorHandling -----

      ...
   MainFunctions Here
   ...
(princ)
); end of Sample 4a


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.