Two types of error handling: One is to redefine the error function, and one is to capture the error.
- ;; ===================================================================================================
- ;; ===================================================================================================
- ;; =========================================== two kinds of error handling methods ========================================
- ;; ===================================================================================================
- ;; ===================================================================================================
- ;; =============================================01. Error function ===========================================
- (defun c:tt()
- ;; Backup system error Function
- (setq *error*_bak *error*)
- ;; Assigning a custom error function to a system error function
- ( setq *error* *error*_non)
- ;; Backup Capture
- (setq osmode_bak ( GetVar "Osmode"))
- ;; ----------------------------------------
- ( setvar "Osmode " 0)
- (getpoint)
- ( command " line" pause pause "")
- ;; ----------------------------------------
- ;; Normal execution also to restore the error function
- ( setq *error* *error*_bak)
- ;; Normal execution also restores the modified variable
- ( setvar "Osmode" osmode_bak)
- )
- ;; Define your own Error function
- (defun *error*_non (msg)
- ;; Cancel execution of the use (command) of the CAD built-in command execution, and then execute the following statement
- (command)
- ;; To restore a system error function
- ( setq *error* *error*_bak)
- ;; Restore modified variables after an error
- ( setvar "Osmode" osmode_bak)
- )
- ;; =============================================02. Error Capture ===========================================
- ;; Function: whether the command exists
- ;; Parameters: Command string
- ;; Return value: exists as T, otherwise nil
- (defun iscommandexist(commandstr / candcommandstr Isexist myvalue)
- (setq candcommandstr (strcat "C:" commandstr))
- (if (Equal (type ( eval (read candcommandstr))) ' subr)
- ;; Description is a command defined with Lisp's Defun .
- (Progn
- (setq isexist T)
- ;(p rint "command exists!")
- )
- ;; Not a defun-defined command in Lisp
- (progn
- (if
- ;; If the condition of the Judgment
- ( not
- ;; Catch error, error exists as T, does not exist as false
- (vl-catch-all-error-p
- ;; 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 .
- (setq myvalue
- ;; 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
- (vl-catch-all-apply'(Lambda (x) (Progn (Commandx) (setqLastcommand(GetVar "Lastprompt")) (if ( not (WcmatchLastcommand"* Unknown command *")) (Command "ESC")))) (ListCommandstr))
- )
- )
- )
- ;; The first statement after the IF condition
- ;; If you perform an error, execute the following sentence
- (progn
- (setq isexist nil)
- ;(p rint "command does not exist!")
- )
- ;; The second statement after the IF condition
- ;; If the execution error occurs, catch the error and execute the following sentence
- (progn
- (setq isexist T)
- ;(p rint "command exists!")
- )
- );; End If
- ;; Cancel command Operation
- (if isexist
- (Progn
- (command)
- )
- );; End If
- );; End Progn
- );; End If
- ;; Output value, the command is t when it is present, otherwise nil
- isexist
- )
Two methods of error handling