Helchan的博客园

导航

【Visual Lisp】两种出错处理方式

两种出错处理方式:一种是对出错函数进行重定义,一种是对错误进行捕捉处理。

 

 

  1. ;;===================================================================================================
  2. ;;===================================================================================================
  3. ;;===========================================两种出错处理方式========================================
  4. ;;===================================================================================================
  5. ;;===================================================================================================
  6. ;;=============================================01.出错函数===========================================
  7. (defun c:tt()
  8.   ;;备份系统出错函数
  9.   (setq *error*_bak *error*)
  10.   ;;将自定义出错函数赋值给系统出错函数
  11.   (setq *error* *error*_non)
  12.   ;;备份捕捉
  13.   (setq osmode_bak (getvar "osmode"))
  14.   
  15.   ;;----------------------------------------
  16.   (setvar "osmode" 0)
  17.   (getpoint)
  18.   (command "line" pause pause "")
  19.   ;;----------------------------------------
  20.   
  21.   ;;正常执行也要还原出错函数
  22.   (setq *error* *error*_bak)
  23.   ;;正常执行也要还原修改的变量
  24.   (setvar "osmode" osmode_bak)
  25. )
  26. ;;定义自己的出错函数
  27. (defun *error*_non (msg)
  28.   ;;对于CAD内置command命令执行的使用(command)取消执行,然后执行后面语句
  29.   (command)
  30.   ;;将系统出错函数进行还原
  31.   (setq *error* *error*_bak)
  32.   ;;出错后还原修改的变量
  33.   (setvar "osmode" osmode_bak)
  34. )
  35. ;;=============================================02.错误捕捉===========================================
  36. ;;功 能:命令是否存在
  37. ;;参 数:命令字符串
  38. ;;返回值:存在为T,否则为nil
  39. (defun isCommandExist(commandstr / candcommandstr isexist myvalue)
  40.   (setq cAndCommandStr (strcat "c:" commandStr))
  41.   (if (equal (type (eval (read cAndCommandStr))) 'subr)
  42.     ;;说明是用lisp的defun定义的命令
  43.     (progn
  44.       (setq isExist T)
  45.       ;(print "命令存在!")
  46.     )
  47.     ;;不是lisp中defun定义的命令
  48.     (progn
  49.       (if
  50.         ;;if判断的条件
  51.         (not
  52.           ;;捕捉错误,错误存在为T,不存在为FALSE
  53.           (vl-catch-all-error-p
  54.             ;;将语句执行结果返回给myvalue,语句执行出错myvalue值为#<%catch-all-apply-error%>,不出错的话就是语句执行后的结果
  55.             (setq myvalue
  56.               ;;执行语句,并且用vl-catch-all-apply捕捉错误,注意后面的格式,第一个为函数注意前面用单引号,后面为函数参数表
  57.               (vl-catch-all-apply '(lambda (x) (progn (command x) (setq lastCommand (getvar "LASTPROMPT")) (if (not (wcmatchlastCommand "*未知命令*")) (command "ESC")))) (list commandStr))
  58.             )
  59.           )
  60.         )
  61.         ;;if条件后的第一条语句
  62.         ;;如果执行出错就执行下面这句
  63.         (progn
  64.           (setq isExist nil)
  65.           ;(print "命令不存在!")
  66.         )
  67.         ;;if条件后的第二条语句
  68.         ;;如果执行出错,就捕捉错误,执行下面这句
  69.         (progn
  70.           (setq isExist T)
  71.           ;(print "命令存在!")
  72.         )
  73.       );;end if
  74.       ;;取消命令操作
  75.       (if isExist
  76.         (progn
  77.           (command)
  78.         )
  79.       );;end if
  80.     );;end progn
  81.   );;end if
  82.   
  83.   ;;输出值,命令存在时是T,否则是nil
  84.   isExist
  85. )

 

posted on 2015-06-07 22:15  Helchan  阅读(3032)  评论(0编辑  收藏  举报