找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1255|回复: 2

[密技]:vl下的出错处理方法:

[复制链接]
发表于 2002-9-13 15:58:18 | 显示全部楼层 |阅读模式

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

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

×

  1. <body>
  2. <table cellSpacing="0" cellPadding="2" width="100%" bgColor="#e0e0e0" border="0">
  3.   <tbody>
  4.     <tr>
  5.       <td><font face="arial,sans-serif">
  6.         <table cellSpacing="0" cellPadding="0" align="right" border="0">
  7.           <tbody>
  8.             <tr>
  9.               <td></td>
  10.             </tr>
  11.           </tbody>
  12.         </table>
  13.         </font>寄件者:<font face="arial,sans-serif"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;q=author:tony.tanzillo%40worldnet.att.net+">Tony
  14.         Tanzillo</a> (<a href="mailto:tony.tanzillo@worldnet.att.net">tony.tanzillo@worldnet.att.net</a>)<br>
  15.         </font>主旨:<font face="arial,sans-serif">Re: Globally Cath
  16.         Exeptions<br>
  17.         </font>新闻群组:<font face="arial,sans-serif"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;group=autodesk.autocad.customization">autodesk.autocad.customization</a><br>
  18.         <table cellSpacing="0" cellPadding="0" align="right" border="0">
  19.           <tbody>
  20.             <tr>
  21.               <td><font face="arial,sans-serif">View: <a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;threadm=37F00043.775B4094%40worldnet.att.net&amp;rnum=2&amp;prev=/groups%3Fhl%3Dzh-CN%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D37F00043.775B4094%2540worldnet.att.net%26rnum%3D2">Complete
  22.                 Thread (6 articles)</a> | <a href="http://groups.google.com/groups?selm=37F00043.775B4094%40worldnet.att.net&amp;oe=UTF-8&amp;output=gplain">Original
  23.                 Format</a></font></td>
  24.             </tr>
  25.           </tbody>
  26.         </table>
  27.         </font>日期:<font face="arial,sans-serif">1999/09/27<br>
  28.         </font></td>
  29.     </tr>
  30.   </tbody>
  31. </table>
  32. <pre>I'm not sure I understand what you mean by 'not clean'.

  33. The very nature of exception handling mandates that
  34. the application specifically prepare for and deal
  35. with exceptions locally, so that it can resume its
  36. execution or terminate gracefully (e.g., clean up).

  37. You have to remember that exceptions are used both
  38. for errors, and for signaling conditions like user-
  39. input was not provided, an option keyword was entered,
  40. or a collection key was not found. These types of
  41. exceptions _must_ be handled locally and explicitly.

  42. If you were to wrap your entire program inside one
  43. exception handler in your main command function, it
  44. would catch the exception, but that's essentially
  45. like using *error*, because the execution of your
  46. code will still be suspended at the point where the
  47. exception occurred, and control will be transferred
  48. to the next expression that follows the protected
  49. expression (the code passed to vl-catch-all-apply):

  50. <font color="#0000FF" face="Courier New">(defun C:YOURCOMMAND ( / rslt)
  51.    (setq rslt
  52.       (vl-catch-all-apply
  53.         '(lambda ()
  54.             (run-your-application)
  55.          )
  56.       )
  57.    )
  58.    (if (vl-catch-all-error-p rslt)
  59.       (alert
  60.          (strcat
  61.             &quot;This expression is evaluated immediately &quot;
  62.             &quot;after the one in which the exception occurred&quot;
  63.          )
  64.       )
  65.       rslt
  66.    )
  67. )</font>

  68. The above is really no different than this:

  69. <font color="#0000FF" face="Courier New">(defun C:YOURCOMMAND ( / *error*)
  70.    (defun *error* (msg)
  71.       (alert
  72.          (strcat
  73.            &quot;This expression is evaluated immediately &quot;
  74.            &quot;after the one in which the exception occurred&quot;
  75.          )
  76.       )
  77.    )
  78.    (run-your-application)
  79. )</font>

  80. So, what good is the former?

  81. There are some tricks that a very skilled programmer
  82. can do, to wrap the body of every function in their
  83. program in its own exception handler, but that doesn't
  84. really solve the problem, other than perhaps serving
  85. as a debugging tool. That's because the way you handle
  86. an exception depends on both the nature of the error,
  87. and how/where/when/why it occurs.

  88. Here's a helper function that can make localized
  89. exception handling a bit less frustrating:

  90. <font color="#0000FF" face="Courier New">;; (catch-error &lt;Protected&gt; &lt;OnError&gt;)
  91. ;;
  92. ;; (catch-error
  93. ;;   '(lambda () &lt;code to protect&gt;...)
  94. ;;   '(lambda (error) &lt;error handling code&gt;...)
  95. ;; )
  96. ;;
  97. ;; A wrapper for vl-catch-all-xxxxxxx
  98. ;;
  99. ;; This function provides a high-level exception-handling
  100. ;; construct that allows the programmer to factor out some
  101. ;; of the testing and flow control required when using the
  102. ;; vl-catch-all-xxxxx functions directly (which is messy).
  103. ;;
  104. ;; (catch-error) takes two arguments, both of which are
  105. ;; quoted function names or lambda lists:
  106. ;;
  107. ;; &lt;Protected&gt; is a function that takes no arguments
  108. ;; (usually a lambda list). It is evaluated, and if an
  109. ;; error occurs during its evaluation, then &lt;OnError&gt; is
  110. ;; called and passed the error string.
  111. ;;
  112. ;; &lt;OnError&gt; is a function that is called only if an error
  113. ;; occurs during the evaluation of &lt;Protected&gt;. In that case,
  114. ;; the message returned by (vl-catch-all-error-message) is
  115. ;; passed to &lt;OnError&gt; as its only argument.
  116. ;;
  117. ;; If no error occurs during evaluation of &lt;Protected&gt;, then
  118. ;; (catch-error) returns the result of &lt;Protected&gt;. If an error
  119. ;; occurs during evaluation of &lt;Protected&gt;, then (catch-error)
  120. ;; returns the result of &lt;OnError&gt; if supplied. If an error
  121. ;; occurs and &lt;OnError&gt; is not supplied (e.g., nil), then
  122. ;; (catch-error) returns the Visual LISP error object that
  123. ;; triggered the error.
  124. ;;
  125. ;; By default, errors are always handled. The only way to
  126. ;; re-raise the error is to call (exit) from within &lt;OnError&gt;,
  127. ;; and is generally what should be done in cases where the
  128. ;; error is unknown or the code in the &lt;OnError&gt; error handler
  129. ;; is not equipped to deal with a particular type of error.
  130. ;;
  131. ;; Note that calls to (catch-error) can be nested, such that
  132. ;; if a nested call does not handle an error (by calling exit
  133. ;; or vl-exit-with-error), then one or more outer calls can
  134. ;; also examine and/or handle the error, with the caveat that
  135. ;; the error message is not the same as the original one, if
  136. ;; you use (exit). If you use (vl-exit-with-error), then the
  137. ;; error message is correct, but in that case the outer most
  138. ;; call to (catch-all) must handle the error because it cannot
  139. ;; call (exit) to invoke the *error* function.
  140. ;;
  141. ;; In essence, you can think of each call to
  142. ;; catch error to be a temporary and locally-bound
  143. ;; redefinition of the *error* function (where the
  144. ;; &lt;OnError&gt; argument represents the body of *error*),
  145. ;; with the added benefit of being able to resume
  146. ;; execution if desired.
  147. ;;
  148. ;;
  149. ;; Example:
  150. ;;
  151. ;;   The following code obtains a point or the
  152. ;;   option keyword &quot;foo&quot;, and traps the error
  153. ;;   that occurs if the user responds with the
  154. ;;   option keyword.
  155. ;;   
  156. ;;   If the error is not a result of the user
  157. ;;   entering the keyword, then something else
  158. ;;   has gone wrong. In that case, the error
  159. ;;   handler displays the error message, and
  160. ;;   aborts by calling (exit).
  161. ;;   
  162. ;;   If no error occurs, then the error handler
  163. ;;   is not called, and the user-supplied point
  164. ;;   is assigned to Result
  165. ;;
  166. ;; (defun C:CATCH-ERROR-TEST ()
  167. ;;   
  168. ;;   (setq eKeyWordInput
  169. ;;      &quot;Automation Error. User input is a keyword&quot;
  170. ;;   )
  171. ;;   
  172. ;;   (setq Util
  173. ;;      (vla-get-utility
  174. ;;         (vla-get-activeDocument
  175. ;;            (vlax-get-acad-object)
  176. ;;         )
  177. ;;      )
  178. ;;   )
  179. ;;   
  180. ;;
  181. ;;   (vla-IntializeUserInput Util 0 &quot;Foo&quot;)
  182. ;;   
  183. ;;   (setq result
  184. ;;      (catch-error
  185. ;;           '(lambda ()
  186. ;;            (vla-Getpoint
  187. ;;               Util nil &quot;Enter point/Foo: &quot;
  188. ;;            )
  189. ;;         )
  190. ;;           '(lambda (error)
  191. ;;               (if (eq error eKeyWordInput)
  192. ;;                  (vla-GetInput Util)
  193. ;;                  (progn
  194. ;;                     (alert (strcat &quot;Error: &quot; errmsg))
  195. ;;                     (exit)
  196. ;;                  )
  197. ;;               )
  198. ;;            )
  199. ;;         )
  200. ;;   )
  201. ;;           
  202. ;;   (if (eq (type Result) 'Str)
  203. ;;      (princ &quot;\nUser entered 'FOO'&quot;)
  204. ;;      (princ &quot;\nUser entered a point&quot;)
  205. ;;   )
  206. ;;   
  207. ;;   (princ)
  208. ;;
  209. ;; )
  210. ;;              

  211. (defun catch-error (catch:Protected catch:OnError / catch:err)
  212.     (setq catch:err
  213.        (vl-catch-all-apply catch:Protected)
  214.     )
  215.     (if (and (vl-catch-all-error-p catch:err)
  216.              catch:OnError
  217.         )
  218.         (Apply catch:OnError
  219.            (list (vl-catch-all-error-message catch:err))
  220.         )
  221.         catch:err
  222.     )
  223. )

  224. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  225. </font></pre>

  226. </body>
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 23个

财富等级: 恭喜发财

发表于 2002-9-14 22:50:14 | 显示全部楼层
Tony 可能是这个世界上最出色的AutoCAD开发者之一了,他的书和文章太好了!吐血推荐!!!!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2002-12-6 17:28:24 | 显示全部楼层
可是不知道哪里有买?国内的书大部分是help的印刷版本。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-26 03:45 , Processed in 0.172842 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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