Sweety 发表于 2022-7-6 10:16:47

什么';如果用户按

大家好
 
假设我有以下例程,用户在完成Lisp之前按下了Cancel。
 
(setq os (getvar 'osmode))
(setvar 'osmode 0)
............. Here is the rest of codes
..... So the user Canceled the Lisp

(setvar 'osmode os) ; <<-- This System Variable won't be re-set as it was before.

 
要还原的示例

(setq SUS_LIST (list "cmdecho" "orthomode" "osmode" "blipmode" "clayer" "snapang" "textsize" "textstyle" "angbase" "angdir")
       SUS      (mapcar 'getvar SUS_LIST)
       TERR *error*
      *error* MD_SET)

 
例如,如果用户按下escape或发生错误。

(defun MD_RUS ()
(setq *error* TERR)
(if SUS (mapcar 'setvar SUS_LIST SUS))
(princ "\nMechanical Duct - Imperial, Copyright © 2010")
(princ "\nThe program will now restore the user settings and exit.")
(princ))

The Buzzard 发表于 2022-7-6 10:23:03

看看这里
http://www.afralisp.net/autolisp/tutorials/error-trapping.php

lpseifert 发表于 2022-7-6 10:24:43

 
这就是我得到上面这个的地方。
打得好,拉里。

The Buzzard 发表于 2022-7-6 10:27:09

这是错误捕获的链接
 
http://www.afralisp.net/archive/lispa/lisp6.htm

The Buzzard 发表于 2022-7-6 10:30:32

当我第一次了解他们时,我发现罗伯特·贝尔的错误处理程序是一个很好的起点;从非盟课程中学习:
 

(defun MD_SET (ERRORMSG)
(command nil nil nil)
(if (not (member ERRORMSG '("console break" "Function cancelled")))
   (princ (strcat "\nError:" ERRORMSG)))
(if SUS (mapcar 'setvar SUS_LIST SUS))
(princ "\nAttention!....A user error has occurred.")
(princ "\nThe program will now restore the user settings and exit.")
(terpri)
(setq *error* TERR)
(princ))

BlackBox 发表于 2022-7-6 10:34:09

我使用的。。。

(defun *error* (msg)
(cond ((not msg))                                              ; Normal exit
   ((member msg '("Function cancelled" "quit / exit abort")))   ; <esc> or (quit)
   ((princ (strcat "\n<!>Error: " msg "<!> "))            ; Fatal error, display it
    (cond ((vl-bb-ref '*Debug*) (vl-bt)))))                     ; If in debug mode, dump backtrace
(princ))

alanjt 发表于 2022-7-6 10:37:47

 
谢谢大家,我的回复很好。
 
所以我发现Renderman先生支持的代码对我来说是最容易处理的,所以会是这样吗。。。
 
(defun *error* (msg)
(if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,")))
   (princ (strcat "\nError: " msg))
)
)
 
因为如果用户按下cancel,例程将重新设置error defun中包含的系统变量。
 
是这样吗?
 
再次感谢大家。

Sweety 发表于 2022-7-6 10:41:30

别忘了本地化*error*函数!!!

alanjt 发表于 2022-7-6 10:44:57

这就是你说的艾伦先生吗。?
(defun *error* (msg)
(cond ((not msg))                                              ; Normal exit
   ((member msg '("Function cancelled" "quit / exit abort")))   ; <esc> or (quit)
   ((princ (strcat "\n<!>Error: " msg "<!> "))            ; Fatal error, display it
    (cond ((vl-bb-ref '*Debug*) (vl-bt)))))                     ; If in debug mode, dump backtrace
   (setvar 'osmode os)
(princ))

(setq os (getvar 'osmode))
(setvar 'osmode 0)
..........
.............. code of the routine

(setvar 'osmode os)

Sweety 发表于 2022-7-6 10:46:28

不,在实际例程中本地化*error*函数本身。
 
如。
(defun *error* (msg / os)
(cond ((not msg))                                              ; Normal exit
   ((member msg '("Function cancelled" "quit / exit abort")))   ; <esc> or (quit)
   ((princ (strcat "\n<!>Error: " msg "<!> "))            ; Fatal error, display it
    (cond ((vl-bb-ref '*Debug*) (vl-bt)))))                     ; If in debug mode, dump backtrace
   (setvar 'osmode os)
(princ))

(setq os (getvar 'osmode))
(setvar 'osmode 0)
..........
.............. code of the routine

(setvar 'osmode os)
页: [1] 2
查看完整版本: 什么';如果用户按