Two methods of error handling

Source: Internet
Author: User

Two types of error handling: One is to redefine the error function, and one is to capture the error.

  1. ;; ===================================================================================================
  2. ;; ===================================================================================================
  3. ;; =========================================== two kinds of error handling methods ========================================
  4. ;; ===================================================================================================
  5. ;; ===================================================================================================
  6. ;; =============================================01. Error function ===========================================
  7. (defun c:tt()
  8.   ;; Backup system error Function
  9. (setq *error*_bak *error*)
  10.   ;; Assigning a custom error function to a system error function
  11. ( setq *error* *error*_non)
  12.   ;; Backup Capture
  13. (setq osmode_bak ( GetVar "Osmode"))
  14.   ;; ----------------------------------------
  15. ( setvar "Osmode " 0)
  16. (getpoint)
  17. ( command " line" pause pause "")
  18.   ;; ----------------------------------------
  19.   ;; Normal execution also to restore the error function
  20. ( setq *error* *error*_bak)
  21.   ;; Normal execution also restores the modified variable
  22. ( setvar "Osmode" osmode_bak)
  23. )
  24. ;; Define your own Error function
  25. (defun *error*_non (msg)
  26.   ;; Cancel execution of the use (command) of the CAD built-in command execution, and then execute the following statement
  27. (command)
  28.   ;; To restore a system error function
  29. ( setq *error* *error*_bak)
  30.   ;; Restore modified variables after an error
  31. ( setvar "Osmode" osmode_bak)
  32. )
  33. ;; =============================================02. Error Capture ===========================================
  34. ;; Function: whether the command exists
  35. ;; Parameters: Command string
  36. ;; Return value: exists as T, otherwise nil
  37. (defun iscommandexist(commandstr / candcommandstr Isexist myvalue)
  38. (setq candcommandstr (strcat "C:" commandstr))
  39. (if (Equal (type ( eval (read candcommandstr))) ' subr)
  40.     ;; Description is a command defined with Lisp's Defun .
  41. (Progn
  42. (setq isexist T)
  43. ;(p rint "command exists!")
  44.     )
  45.     ;; Not a defun-defined command in Lisp
  46. (progn
  47. (if
  48.         ;; If the condition of the Judgment
  49. ( not
  50.           ;; Catch error, error exists as T, does not exist as false
  51. (vl-catch-all-error-p
  52.             ;; The statement execution results are returned to myvalue, and the statement execution error myvalue value is #<%catch-all-apply-error%>, and no error is the result of the statement execution .
  53. (setq myvalue
  54.               ;; Execute the statement, and catch the error with vl-catch-all-apply, note the following format, first for the function note preceded by a single quote, followed by the function parameter table
  55.               (vl-catch-all-apply'(Lambda (x) (Progn (Commandx) (setqLastcommand(GetVar "Lastprompt")) (if ( not (WcmatchLastcommand"* Unknown command *")) (Command "ESC")))) (ListCommandstr))
  56.             )
  57.           )
  58.         )
  59.         ;; The first statement after the IF condition
  60.         ;; If you perform an error, execute the following sentence
  61. (progn
  62. (setq isexist nil)
  63. ;(p rint "command does not exist!")
  64.         )
  65.         ;; The second statement after the IF condition
  66.         ;; If the execution error occurs, catch the error and execute the following sentence
  67. (progn
  68. (setq isexist T)
  69. ;(p rint "command exists!")
  70.         )
  71.       );; End If
  72.       ;; Cancel command Operation
  73. (if isexist
  74. (Progn
  75. (command)
  76.         )
  77.       );; End If
  78.     );; End Progn
  79.   );; End If
  80.   ;; Output value, the command is t when it is present, otherwise nil
  81. isexist
  82. )

Two methods of error handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.