找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 966|回复: 2

程序出错继续执行方法

[复制链接]
发表于 2004-11-30 15:39:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
下面是一个简单的加载一个lisp文件夹中的所有程序文件的函数:
(defun fi-loadlisp-in-a-directory (directoryname / filelist)
  (setq filelist (vl-directory-files directoryname  nil 1));the the name list
  (mapcar   '(lambda (a) (load (strcat directoryname a))) filelist );load each lsp
  );defun
但是如果遇到写的不完善的程序,运行就会中断,以致余下的程序文件也无法加载,如何做到能跳过继续运行呢?在cad2002的帮助中见到有关vl-catch-all-apply 函数,有举例如下,但仍不知以上问题如何解决。
   The following code defines a function named catch-me-if-you-can. Copy the code into a VLISP text editor window:
(defun catch-me-if-you-can (dividend divisor / errobj)
  (setq errobj (vl-catch-all-apply '/ (list dividend divisor)))
  (if (vl-catch-all-error-p errobj)
    (progn
      (print (strcat "An error occurred: "
                     (vl-catch-all-error-message errobj)
             )
      )
      (prompt "Do you want to continue? (Y/N) -> ")
      (setq ans (getstring))
      (if (equal (strcase ans) "Y")
        (print "Okay, I'll keep going")
      )
    )
    (print errobj)
  )
  (princ)
)
This function accepts two number arguments and uses vl-catch-all-apply to divide the first number by the second number. The vl-catch-all-error-p function determines whether the return value from vl-catch-all-apply is an error object. If the return value is an error object, catch-me-if-you-can invokes vl-catch-all-error-message to obtain the message from the error object.

Load the function.
Invoke the function with the following command:
(catch-me-if-you-can 50 2)
The function should return 25.

Intentionally cause an error condition by invoking the function with the following command:
(catch-me-if-you-can 50 0)
The function should issue the following prompt in the AutoCAD Command window:

"An error occurred: divide by zero" Do you want to continue? (Y/N) ->
If you enter y, catch-me-if-you-can indicates that it will continue processing.

Try modifying this example by changing vl-catch-all-apply to apply. Load and run the example with a divide by zero again. When apply results in an error, execution immediately halts and *error* is called, resulting in an error message.
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 488个

财富等级: 日进斗金

发表于 2004-11-30 23:28:17 | 显示全部楼层
(mapcar '(lambda (a) (vl-catch-all-apply  'load (list (strcat directoryname a)))) filelist )
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2004-12-1 22:57:26 | 显示全部楼层

谢谢斑竹

制作了一个lsp,fas加载函数与大家分享:

;;;(fi_loadlisp_in_a_directory  dir);加载一个文件夹中的所有LSP文件,dir为一文件路径字符串,如:"C:/zlisp";;
;;;作者: xdcad.星星  2004年12月1日;;;
;;;此文件可以全文可以添加在acad2000.lsp文件中,并在其后添加如:(fi_loadlisp_in_a_directory "C:/zlisp")
;;;从而实现自动加载文件夹中的所有LSP文件;;;
;;;本函数与fi-AUTOLOAD相比只加载直接目录下的文件,而fi_autoload则加载所有各级子目录中的所有fas和lap文件;;
;;;在cad2000中自动加载文件的工具,这样省去autoload的麻烦,
;;;;文件可以加载则加载,如果不能则跳过,并且能够统计一共有哪些文件未被加载,
(DEFUN fi_loadlisp_in_a_directory        (dir / FI FILI ERRLI)
  (setvar "cmdecho" 0)
  (setq errli(list))
  (SETQ FILI (vl-directory-files dir))
  (mapcar '(lambda (fi / fufi)
             (setq fufi(strcat dir "/" fi))
             (IF (OR (= (VL-FILENAME-EXTENSION FI) ".LSP")
                     (= (VL-FILENAME-EXTENSION FI) ".lsp")
                     (= (VL-FILENAME-EXTENSION FI) ".FAS")
                     (= (VL-FILENAME-EXTENSION FI) ".fas")
                 );过滤出lsp,fas文件;;;
(if (vl-catch-all-error-p (vl-catch-all-apply 'load (list fufi)))(setq errli(cons fufi errli)))
             )                                ;IF
           )
          FILI
  )
  (PRINC "以下函数出现加载错误:")
  (setq errli errli)
)
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-27 17:33 , Processed in 0.370231 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表