See the top rated post in this thread. Click here

Results 1 to 5 of 5

Thread: Error Handling:

  1. #1
    Active Member spencer.67965's Avatar
    Join Date
    2004-05
    Posts
    76
    Login to Give a bone
    0

    Default Error Handling:

    Hello All,

    Just wondering if there is any error handling in AutoLISP; if so could someone give me an example. I am trying to exit gracefully during a routine when I press the "ESC" Button.

    Thanks,

    Spencer
    ___________________
    AutoCAD 2005

  2. #2
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: Error Handling:

    Hi Spencer ! This is my offer . .

    (defun c:MyProg (argument / localvariable OldOsm ) ;; *globalvariable*

    ; Errorhandler
    (defun MyProg_Err ( msg )
    (princ (strcat "User [Esc]. " msg ) )
    (setvar "OSMODE" OldOsm ) ; and many more "take care of"
    (setq *error* OldErr )
    (command "_.UNDO" "End")
    ;(command "_.U") ; sometimes
    )

    ;;; Program Start
    (command "_.UNDO" "BEgin")
    (setq OldErr *error* *error* MyProg_Err )
    (setq OldOsm (getvar "OSMODE" ) ; and many more "take care of"
    ; all your program code

    ;;; Program End
    (command "_.UNDO" "End")
    (setvar "OSMODE" OldOsm ) ; and many more "take care of"
    (setq *error* OldErr )
    (princ)
    )

    Happy Computing !

    kennet

  3. #3
    The Silent Type RobertB's Avatar
    Join Date
    2000-01
    Location
    Seattle WA USA
    Posts
    5,859
    Login to Give a bone
    1

    Default Re: Error Handling:

    Spencer,

    Kennet's reply might be a bit confusing. Let me try with a bit more information.

    *Error* is the traditional error handler for Auto/Visual LISP. Beginning with AutoCAD 2000, the error handler could be declared as a local variable and remain local even when it ran. The (setq OleErr ...) stuff Kennet did is rather old-school, only needed if you must support older versions of AutoCAD (but I digress).

    The error handler can be both global and local. If you do this:
    Command: (defun *error* (msg) (princ "[Global] error: ") (princ msg) (princ))
    You have essentially recreated the default global error handler, with the addition of the "[Global]".
    So, Command: (/ 1 0)
    [Global] error: divide by zero


    Now, to define a local error handler, you simply need to declare it a local in the parent function, and define it inside the parent function.
    Code:
    (defun C:Test  (/ *Error*)
     (defun *Error* (msg) (princ "[Local] error: ") (princ msg) (princ))
     (/ 1 (getint "\nDivide 1 by: ")))
    This code does this when run at the command line:
    Command: test
    Divide 1 by: 1
    1

    Command: test
    Divide 1 by: 0
    [Local] error: divide by zero


    Note that if you trigger an error at the command prompt itself the global error handler still runs.
    Command: (/ 1 0)
    [Global] error: divide by zero


    So, now that you see how to make a local error handler, let's add a trap for cancelling the function.
    Code:
    (defun C:Test  (/ *Error*)
     (defun *Error*  (msg)
      (cond ((not msg)) ; normal exit
            ((member msg '("Function cancelled" "quit / exit abort"))) ; <Esc>
            ((princ (strcat "\n[Local] error: " msg)) ; display fatal error
             (cond (*Debug* (vl-bt))))) ; if in debug mode, dump backtrace
      (princ))
     (/ 1 (getint "\nDivide 1 by: ")))
    Command: test
    Divide 1 by: *Cancel*

    Command: test
    Divide 1 by: 0
    [Local] error: divide by zero


    I hope this further clairified things! (I'll leave it up to you to figure out how the debug mode works, but I bet you'll like it!)

  4. #4
    Active Member spencer.67965's Avatar
    Join Date
    2004-05
    Posts
    76
    Login to Give a bone
    0

    Default Re: Error Handling:

    Thanks for clarifying that for me. I'll give it a go and see if I have any other question.

    Spencer

  5. #5
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: Error Handling:

    Hi again Spencer !

    In my offer I take care of user variables, when a user use a program the program must leave the user environment unchanged when the program is done, even if the program crash or the user hit the [Esc] key.
    And it also step back to the starting point if something goes wrong.
    And if you create many objects with the program it removes them all if you after the program change your mind and type the "Command _U"

    To do
    1. (defun c:MyProg ( / ) ;; MyProg_Err

    or

    2. (defun c:MyProg ( / MyProg_Err )
    is a up to you

    Number 1. has a global error handler and is compatible with R14 and earlier versions
    you can call that error handler in other similar program that you make.

    Number 2. has a internal error handler and is not compatible with R14 and earlier versions
    but save a little bit RAM memory if you have a low low level computer,
    but You can not call that one from other program.

    The error handler itself can be written to just take care, or be unnecessary excessive
    you set the level.

    Happy Computing !

    kennet
    Last edited by kennet.sjoberg; 2004-09-15 at 10:45 PM.

Similar Threads

  1. Error Handling
    By ticad02 in forum AutoLISP
    Replies: 16
    Last Post: 2009-12-21, 03:39 PM
  2. Error Handling
    By whattaz13 in forum AutoLISP
    Replies: 2
    Last Post: 2008-07-16, 01:03 PM
  3. Error Handling
    By jwf in forum AutoCAD Customization
    Replies: 2
    Last Post: 2004-12-03, 07:42 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
  •