Results 1 to 7 of 7

Thread: Error Trapping

  1. #1
    100 Club
    Join Date
    2003-11
    Location
    Dublin, Ireland.
    Posts
    152
    Login to Give a bone
    0

    Default Error Trapping

    I know I should have done this a long time ago, but in a bid to better my programing,
    I am trying to learn about Error Trapping.

    So I have a few questions, and I would be very grateful if someone could point me in the right direction.

    1. Local or Global? Should I try and write one routine that would cover all or should I write one into each routine?

    2. Would I be better trying to catch all system variables and settings (just in case !!!),
    or just the one's I change myself at the start of the program?

    3. As I am trying to learn and use Visual Lisp more and more, would the AutoLisp error trap still work, or do I have to use the "vl-catch-all-apply". (this is where I really get out of my depth)
    Does the "vl-catch-all-apply" only apply to lists? A lot of my routines just carry out modifications to selection sets, so would it apply?

    Has anybody any thoughts? I thought I would throw it out there, as I am loosing the battle between learning and having to work for a living.


    Thanks in advance


    John.

  2. #2
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Chelan WA
    Posts
    1,037
    Login to Give a bone
    0

    Default Re: Error Trapping

    This is my error trap function that I have been presenting at my lectures at Autodesk University for the last couple years. It makes error trapping as painless as I could make it.

    If you wrap "ANY" lisp expression with this function and a quote like

    (errortrap (quote (setq X (/ 1.0 0.0))))

    The errortrap function will return a nil for any error, the result of an expression it is evaluating if it returns a value, and a T if the expression it is evaluating is successful and returns a nil value.

    For debugging, I have included the ability to have the global variable DEBUG set to anything other than nil to print out the error messages if I want to see them.

    The trick is the vlax-catch-all-apply expression requires you to pass it a list to get it to work. I have seen several routines where others were passing the arguments of an expression to be evaluated as the list. In this routine I am utilizing the little used SET function (not setq) to pass the RESULT to the catch-all-apply expression as a list. That way I can pass the entire expression I am trying to evaluate to the function. The quote expression is required to encapsulte the expression to be evaluated.

    Hope it helps.

    Peter


    Code:
    
    ; Standardized Error Trap
    (defun ErrorTrap (symFunction / objError result)
     (if (vl-catch-all-error-p
          (setq objError (vl-catch-all-apply
                         '(lambda (X)(set X (eval symFunction)))
                          (list 'result))))
      (progn
       (if DEBUG
        (princ
         (strcat "\n"
                 (vl-princ-to-string (vl-catch-all-error-message objError))
                 "\nWhile evaluating the expression: "
                 (vl-princ-to-string symfunction)
                 "\n"
         )
        )
       )
       nil  
      )
      (if result result 'T)
     )
    )

  3. #3
    AUGI Addict
    Join Date
    2015-12
    Posts
    2,095
    Login to Give a bone
    0

    Default Re: Error Trapping

    Start with a generic error handler that will provide some basic functionality, like handling users hitting 'esc' as input. It will also give you a better idea on how to handle errors. Later on, you can locally override it for specific circumstances if required. If this is all a one-off user specific custom job, that should work fine. If this is part of a larger systemic program, you should consider a more structured arrangement. For example, consider the overall program flow to generate what errors would be expected, where they would occur, and which errors should be handled in a similar manner. Remember - time spent up front will minimize hair loss later on.

    I use vl-catch-all-apply primarily with vla- type function calls, but its been handy for some regular lisp functions as well where I might encounter an error and don't want to write twenty-some lines of code to double check all possible problems. Its not limited to lists, you use it to call the function then provide the arguments as a list. For example, if the function returns a variable then I might call (setq this_var (vl-catch-all-apply 'testFunction (list arg1 arg2 arg3...))). Then, I call vl-catch-all-error-p to determine if the value is valid or not and use that to control branching to error control or regular program flow.
    If you are going to fly by the seat of your pants, expect friction burns.
    Some say beauty is in the eye of the Beholder... perhaps so, but I've only seen the disintegration beam.
    Everyone else being wrong is not the same thing as being right.

  4. #4
    All AUGI, all the time CAB2k's Avatar
    Join Date
    2016-01
    Location
    Brandon, Florida
    Posts
    687
    Login to Give a bone
    0

    Default Re: Error Trapping

    Peter,
    Hope you don't mind, this is a variation of your error trap.
    I didn't see a down side to simplifying the vl-catch-all-apply.

    Code:
    ; Standardized Error Trap
    ;  This is a error trap by Peter Jamtgaard modified by CAB
    ; This function will trap an error in an expression argument
    ; The syntax is (setq strLayer (errortrap '(vla-get-layer objSelection)))
    ; returns nil if error occurs else returns the resulting value if not nil
    ; if the result is nil & no error then a value of 'T is returned
    
    (defun ErrorTrap (symFunction / result)
     (if (vl-catch-all-error-p
           (setq result (vl-catch-all-apply 'eval (list symFunction))))
       (if DEBUG
        (prompt
         (strcat "\n"
                 (vl-princ-to-string (vl-catch-all-error-message result))
                 "\nWhile evaluating the expression: "
                 (vl-princ-to-string symfunction)
                 "\n"
         )
        )
       )
       (if result result 'T)
     )
    )

  5. #5
    Certifiable AUGI Addict irneb's Avatar
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Error Trapping

    If you don't want to go the VL route of passing any problem code through the vl-catch-all-apply function, you could use the *error* method. Global / local is your choice, I'm just lazy & created a standardized global ErrorTrap function - see attached. So I don't have to rewrite code all the time. Also mine does the set & save vars through the one function call.
    Attached Files Attached Files
    Knowledge is proportional to experience, but wisdom is inversely proportional to ego!
    My little bit of "wisdom": Hind-sight is useless, unless used to improve the next forethought!

  6. #6
    Member
    Join Date
    2007-08
    Posts
    6
    Login to Give a bone
    0

    Default Re: Error Trapping

    Dear CAB,

    I think (vl-catch-all-error-message result) will return a string.So it is not necessery to use

    vl-princ-to-string. Is it rigth?

  7. #7
    All AUGI, all the time CAB2k's Avatar
    Join Date
    2016-01
    Location
    Brandon, Florida
    Posts
    687
    Login to Give a bone
    0

    Default Re: Error Trapping

    Yes you could eliminate the first vl-princ-to-string

Similar Threads

  1. ACADDOC.LSP error trapping
    By jasonp in forum AutoLISP
    Replies: 3
    Last Post: 2014-03-18, 05:43 PM
  2. Questions on error trapping
    By JaCAD in forum AutoLISP
    Replies: 3
    Last Post: 2011-03-04, 04:47 PM
  3. Error Trapping
    By prose in forum AutoLISP
    Replies: 4
    Last Post: 2008-12-05, 10:02 PM
  4. If Statements & error trapping
    By planview in forum AutoLISP
    Replies: 0
    Last Post: 2006-05-29, 08:46 AM
  5. nested error trapping used incorrectly
    By Bob Eardley in forum AutoCAD General
    Replies: 5
    Last Post: 2005-09-14, 10:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •