Exceptions.xhtml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Exception handling</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Threads.xhtml" title="Threads"/>
  10. <link rel="next" href="Control-features.xhtml" title="Control features"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Exception handling" epub:type="subchapter" id="Exceptions">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Exception handling</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>An <em class="firstterm">exception</em> is an object used to signal an error or
  23. other exceptional situation. The program or run-time system
  24. can <em class="firstterm">throw</em> the exception when an error is discovered.
  25. An exception handler is a program construct that registers
  26. an action to handle exceptions when the handler is active.
  27. </p>
  28. <p>If an exception is thrown and not handled then the
  29. read-eval-print-loop will print a stack trace, and bring
  30. you back to the top level prompt.
  31. When not running interactively, an unhandled exception
  32. will normally cause Kawa to be exited.
  33. </p>
  34. <p>In the Scheme exception model (as of R6RS and R7RS),
  35. exception handlers are one-argument procedures that determine
  36. the action the program takes when an exceptional situation is signaled.
  37. The system implicitly maintains a
  38. current exception handler in the dynamic environment.
  39. The program raises an exception by invoking the current
  40. exception handler, passing it an object encapsulating information
  41. about the exception. Any procedure accepting
  42. one argument can serve as an exception handler and any
  43. object can be used to represent an exception.
  44. </p>
  45. <p>The Scheme exception model is implemented on top of the Java VM’s
  46. native exception model where the only objects that
  47. can be thrown are instances of <code class="literal">java.lang.Throwable</code>.
  48. Kawa also provides direct access to this native model,
  49. as well as older Scheme exception models.
  50. </p>
  51. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877955472" class="indexterm"/> <code class="function">with-exception-handler</code> <em class="replaceable"><code>handler</code></em> <em class="replaceable"><code>thunk</code></em></p>
  52. <div class="blockquote">
  53. <blockquote class="blockquote">
  54. <p>It is an error if <em class="replaceable"><code>handler</code></em> does not accept one argument.
  55. It is also an error if <em class="replaceable"><code>thunk</code></em> does not accept zero arguments.
  56. The <code class="literal">with-exception-handler</code> procedure returns the results
  57. of invoking <em class="replaceable"><code>thunk</code></em>. The <em class="replaceable"><code>handler</code></em> is installed as the current
  58. exception handler in the dynamic environment used for the
  59. invocation of <em class="replaceable"><code>thunk</code></em>.
  60. </p>
  61. <pre class="screen">(call-with-current-continuation
  62. (lambda (k)
  63. (with-exception-handler
  64. (lambda (x)
  65. (display "condition: ")
  66. (write x)
  67. (newline)
  68. (k 'exception))
  69. (lambda ()
  70. (+ 1 (raise ’an-error))))))
  71. ⇒ exception
  72. <span class="emphasis"><em>and prints</em></span> condition: an-error
  73. </pre>
  74. <pre class="screen">(with-exception-handler
  75. (lambda (x)
  76. (display "something went wrong\n"))
  77. (lambda ()
  78. (+ 1 (raise ’an-error))))
  79. <span class="emphasis"><em>prints</em></span> something went wrong
  80. </pre>
  81. <p>After printing, the second example then raises another exception.
  82. </p>
  83. <p><span class="emphasis"><em>Performance note:</em></span> The <em class="replaceable"><code>thunk</code></em> is inlined if it is a
  84. lambda expression. However, the <em class="replaceable"><code>handler</code></em> cannot be inlined
  85. even if it is a lambda expression, because it could be called by
  86. <code class="literal">raise-continuable</code>. Using the <code class="literal">guard</code> form is
  87. usually more efficient.
  88. </p>
  89. </blockquote>
  90. </div>
  91. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877943360" class="indexterm"/> <code class="function">raise</code> <em class="replaceable"><code>obj</code></em></p>
  92. <div class="blockquote">
  93. <blockquote class="blockquote">
  94. <p>Raises an exception by invoking the current exception handler on <em class="replaceable"><code>obj</code></em>.
  95. The handler is called with the same dynamic
  96. environment as that of the call to raise, except that the
  97. current exception handler is the one that was in place when
  98. the handler being called was installed. If the handler returns,
  99. then <em class="replaceable"><code>obj</code></em> is re-raised in the same dynamic environment as the handler.
  100. </p>
  101. <p>If <em class="replaceable"><code>obj</code></em> is an instance of <code class="literal">java.lang.Throwable</code>,
  102. then <code class="literal">raise</code> has the same effect as <code class="literal">primitive-throw</code>.
  103. </p>
  104. </blockquote>
  105. </div>
  106. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877936736" class="indexterm"/> <code class="function">raise-continuable</code> <em class="replaceable"><code>obj</code></em></p>
  107. <div class="blockquote">
  108. <blockquote class="blockquote">
  109. <p>Raises an exception by invoking the current exception handler on <em class="replaceable"><code>obj</code></em>.
  110. The handler is called with the same dynamic
  111. environment as the call to <code class="literal">raise-continuable</code>, except
  112. that: (1) the current exception handler is the one that was
  113. in place when the handler being called was installed, and
  114. (2) if the handler being called returns, then it will again
  115. become the current exception handler.
  116. If the handler returns, the values it returns become the values
  117. returned by the call to <code class="literal">raise-continuable</code>.
  118. </p>
  119. <pre class="screen">(with-exception-handler
  120. (lambda (con)
  121. (cond
  122. ((string? con)
  123. (display con))
  124. (else
  125. (display "a warning has been issued")))
  126. 42)
  127. (lambda ()
  128. (+ (raise-continuable "should be a number")
  129. 23)))
  130. <span class="emphasis"><em>prints:</em></span> should be a number
  131. ⇒ 65
  132. </pre>
  133. </blockquote>
  134. </div>
  135. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667877930560" class="indexterm"/> <code class="function">guard</code> <em class="replaceable"><code><em class="replaceable"><code>variable</code></em></code></em> <em class="replaceable"><code><a class="link" href="Conditionals.xhtml#meta-cond-clause"><em class="replaceable"><code>cond-clause</code></em></a></code></em><em class="replaceable"><code><sup>+</sup></code></em> <em class="replaceable"><code><a class="link" href="Bodies.xhtml#meta-body"><em class="replaceable"><code>body</code></em></a></code></em></p>
  136. <div class="blockquote">
  137. <blockquote class="blockquote">
  138. <p>The <em class="replaceable"><code>body</code></em> is evaluated with an exception handler that binds
  139. the raised object to <em class="replaceable"><code>variable</code></em> and, within the scope of that binding,
  140. evaluates the clauses as if they were the clauses of a <code class="literal">cond</code> expression.
  141. That implicit <code class="literal">cond</code> expression is evaluated with
  142. the continuation and dynamic environment of the <code class="literal">guard</code>
  143. expression. If every cond-clause’s test evaluates to <code class="literal">#f</code>
  144. and there is no <code class="literal">else</code> clause, then <code class="literal">raise-continuable</code> is
  145. invoked on the raised object within the dynamic environment of the
  146. original call to <code class="literal">raise</code> or <code class="literal">raise-continuable</code>,
  147. except that the current exception handler is that of the
  148. <code class="literal">guard</code> expression.
  149. </p>
  150. <pre class="screen">(guard (condition
  151. ((assq 'a condition) =&gt; cdr)
  152. ((assq 'b condition)))
  153. (raise (list (cons 'a 42))))
  154. ⇒ 42
  155. </pre>
  156. <pre class="screen">(guard (condition
  157. ((assq 'a condition) =&gt; cdr)
  158. ((assq 'b condition)))
  159. (raise (list (cons 'b 23))))
  160. ⇒ (b . 23)
  161. </pre>
  162. <p><span class="emphasis"><em>Performance note:</em></span> Using <code class="literal">guard</code> is moderately efficient:
  163. there is some overhead compared to using native exception handling,
  164. but both the <em class="replaceable"><code>body</code></em> and the handlers in the <em class="replaceable"><code>cond-clause</code></em>
  165. are inlined.
  166. </p>
  167. </blockquote>
  168. </div>
  169. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877915776" class="indexterm"/> <code class="function">dynamic-wind</code> <em class="replaceable"><code>in-guard</code></em> <em class="replaceable"><code>thunk</code></em> <em class="replaceable"><code>out-guard</code></em></p>
  170. <div class="blockquote">
  171. <blockquote class="blockquote">
  172. <p>All three arguments must be 0-argument procedures.
  173. First calls <em class="replaceable"><code>in-guard</code></em>, then <em class="replaceable"><code>thunk</code></em>, then <em class="replaceable"><code>out-guard</code></em>.
  174. The result of the expression is that of <em class="replaceable"><code>thunk</code></em>.
  175. If <em class="replaceable"><code>thunk</code></em> is exited abnormally (by throwing an exception or
  176. invoking a continuation), <em class="replaceable"><code>out-guard</code></em> is called.
  177. </p>
  178. <p>If the continuation of the dynamic-wind is re-entered (which
  179. is not yet possible in Kawa), the <em class="replaceable"><code>in-guard</code></em> is called again.
  180. </p>
  181. <p>This function was added in R5RS.
  182. </p>
  183. </blockquote>
  184. </div>
  185. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877907360" class="indexterm"/> <code class="function">read-error?</code> <em class="replaceable"><code>obj</code></em></p>
  186. <div class="blockquote">
  187. <blockquote class="blockquote">
  188. <p>Returns #t if <em class="replaceable"><code>obj</code></em> is an object raised by the <code class="literal">read</code> procedure.
  189. (That is if <em class="replaceable"><code>obj</code></em> is a <code class="literal">gnu.text.SyntaxException</code>.)
  190. </p>
  191. </blockquote>
  192. </div>
  193. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877902272" class="indexterm"/> <code class="function">file-error?</code> <em class="replaceable"><code>obj</code></em></p>
  194. <div class="blockquote">
  195. <blockquote class="blockquote">
  196. <p>Returns #t if <em class="replaceable"><code>obj</code></em> is an object raised by inability to open an input
  197. or output port on a file.
  198. (This includes <code class="literal">java.io.FileNotFoundException</code> as well
  199. as certain other exceptions.)
  200. </p>
  201. </blockquote>
  202. </div>
  203. <section class="sect2" title="Simple error objects" epub:type="division" id="idm139667877898080">
  204. <div class="titlepage">
  205. <div>
  206. <div>
  207. <h3 class="title">Simple error objects</h3>
  208. </div>
  209. </div>
  210. </div>
  211. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877897008" class="indexterm"/> <code class="function">error</code> <em class="replaceable"><code>message</code></em> <em class="replaceable"><code>obj</code></em> <em class="replaceable"><code>...</code></em></p>
  212. <div class="blockquote">
  213. <blockquote class="blockquote">
  214. <p>Raises an exception as if by calling <code class="literal">raise</code>
  215. on a newly allocated <em class="firstterm">simple error object</em>,
  216. which encapsulates the information provided by <em class="replaceable"><code>message</code></em>
  217. (which should a string), as well as any <em class="replaceable"><code>obj</code></em> arguments,
  218. known as the irritants.
  219. </p>
  220. <p>The string representation of a simple error object is as if calling
  221. <code class="literal">(format "#&lt;ERROR ~a~{ ~w~}&gt;" <em class="replaceable"><code>message</code></em> <em class="replaceable"><code>irritants</code></em>)</code>.
  222. (That is the <em class="replaceable"><code>message</code></em> is formatted as if with <code class="literal">display</code>
  223. while each irritant <em class="replaceable"><code>obj</code></em> is formatted as if with <code class="literal">write</code>.)
  224. </p>
  225. <p>This procedure is part of SRFI-23, and R7RS.
  226. It differs from (and is incompatible with) R6RS’s <code class="literal">error</code> procedure.
  227. </p>
  228. </blockquote>
  229. </div>
  230. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877886416" class="indexterm"/> <code class="function">error-object?</code> <em class="replaceable"><code>obj</code></em></p>
  231. <div class="blockquote">
  232. <blockquote class="blockquote">
  233. <p>Returns <code class="literal">#t</code> if <em class="replaceable"><code>obj</code></em> is a simple error object.
  234. Specifically, that <em class="replaceable"><code>obj</code></em> is an instance of <code class="literal">kawa.lang.NamedException</code>.
  235. Otherwise, it returns <code class="literal">#f</code>.
  236. </p>
  237. </blockquote>
  238. </div>
  239. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877880928" class="indexterm"/> <code class="function">error-object-message</code> <em class="replaceable"><code>error-object</code></em></p>
  240. <div class="blockquote">
  241. <blockquote class="blockquote">
  242. <p>Returns the message encapsulated by error-object,
  243. which must be a simple error object.
  244. </p>
  245. </blockquote>
  246. </div>
  247. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877877488" class="indexterm"/> <code class="function">error-object-irritants</code> <em class="replaceable"><code>error-object</code></em></p>
  248. <div class="blockquote">
  249. <blockquote class="blockquote">
  250. <p>Returns a list of the irritants (other arguments)
  251. encapsulated by error-object, which must be a simple error object.
  252. </p>
  253. </blockquote>
  254. </div>
  255. </section>
  256. <section class="sect2" title="Named exceptions" epub:type="division" id="idm139667877874016">
  257. <div class="titlepage">
  258. <div>
  259. <div>
  260. <h3 class="title">Named exceptions</h3>
  261. </div>
  262. </div>
  263. </div>
  264. <p>These functions associate a symbol with exceptions
  265. and handlers: A handler catches an exception if the symbol matches.
  266. </p>
  267. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877872432" class="indexterm"/> <code class="function">catch</code> <em class="replaceable"><code>key</code></em> <em class="replaceable"><code>thunk</code></em> <em class="replaceable"><code>handler</code></em></p>
  268. <div class="blockquote">
  269. <blockquote class="blockquote">
  270. <p>Invoke <em class="replaceable"><code>thunk</code></em> in the dynamic context of <em class="replaceable"><code>handler</code></em> for
  271. exceptions matching <em class="replaceable"><code>key</code></em>. If thunk throws to the symbol <em class="replaceable"><code>key</code></em>,
  272. then <em class="replaceable"><code>handler</code></em> is invoked this way:
  273. </p>
  274. <pre class="screen">(handler key args ...)
  275. </pre>
  276. <p><em class="replaceable"><code>key</code></em> may be a symbol. The <em class="replaceable"><code>thunk</code></em> takes no
  277. arguments. If <em class="replaceable"><code>thunk</code></em> returns normally, that is the return value of
  278. <code class="literal">catch</code>.
  279. </p>
  280. <p>Handler is invoked outside the scope of its own <code class="literal">catch</code>. If
  281. <em class="replaceable"><code>handler</code></em> again throws to the same key, a new handler from further
  282. up the call chain is invoked.
  283. </p>
  284. <p>If the key is <code class="literal">#t</code>, then a throw to <span class="emphasis"><em>any</em></span> symbol will match
  285. this call to <code class="literal">catch</code>.
  286. </p>
  287. </blockquote>
  288. </div>
  289. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877860480" class="indexterm"/> <code class="function">throw</code> <em class="replaceable"><code>key</code></em> <em class="replaceable"><code>arg</code></em> <em class="replaceable"><code>...</code></em></p>
  290. <div class="blockquote">
  291. <blockquote class="blockquote">
  292. <p>Invoke the catch form matching <em class="replaceable"><code>key</code></em>, passing the <em class="replaceable"><code>arg</code></em>s to the
  293. current <em class="replaceable"><code>handler</code></em>.
  294. </p>
  295. <p>If the key is a symbol it will match catches of the same
  296. symbol or of <code class="literal">#t</code>.
  297. </p>
  298. <p>If there is no handler at all, an error is signaled.
  299. </p>
  300. </blockquote>
  301. </div>
  302. </section>
  303. <section class="sect2" title="Native exception handling" epub:type="division" id="idm139667877853776">
  304. <div class="titlepage">
  305. <div>
  306. <div>
  307. <h3 class="title">Native exception handling</h3>
  308. </div>
  309. </div>
  310. </div>
  311. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877852688" class="indexterm"/> <code class="function">primitive-throw</code> <em class="replaceable"><code>exception</code></em></p>
  312. <div class="blockquote">
  313. <blockquote class="blockquote">
  314. <p>Throws the <em class="replaceable"><code>exception</code></em>, which must be an instance of a sub-class
  315. of <code class="literal">java.lang.Throwable</code>.
  316. </p>
  317. </blockquote>
  318. </div>
  319. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667877848416" class="indexterm"/> <code class="function">try-finally</code> <em class="replaceable"><code>body</code></em> <em class="replaceable"><code>handler</code></em></p>
  320. <div class="blockquote">
  321. <blockquote class="blockquote">
  322. <p>Evaluate <em class="replaceable"><code>body</code></em>, and return its result.
  323. However, before it returns, evaluate <em class="replaceable"><code>handler</code></em>.
  324. Even if <em class="replaceable"><code>body</code></em> returns abnormally (by throwing an exception),
  325. <em class="replaceable"><code>handler</code></em> is evaluated.
  326. </p>
  327. <p>(This is implemented just like Java’s <code class="literal">try</code>-<code class="literal">finally</code>.
  328. However, the current implementation does not duplicate the <em class="replaceable"><code>handler</code></em>.)
  329. </p>
  330. </blockquote>
  331. </div>
  332. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667877840976" class="indexterm"/> <code class="function">try-catch</code> <em class="replaceable"><code>body</code></em> <em class="replaceable"><code>handler</code></em> <em class="replaceable"><code>...</code></em></p>
  333. <div class="blockquote">
  334. <blockquote class="blockquote">
  335. <p>Evaluate <em class="replaceable"><code>body</code></em>, in the context of the given <em class="replaceable"><code>handler</code></em> specifications.
  336. Each <em class="replaceable"><code>handler</code></em> has the form:
  337. </p>
  338. <pre class="screen"><em class="replaceable"><code>var</code></em> <em class="replaceable"><code>type</code></em> <em class="replaceable"><code>exp</code></em> ...
  339. </pre>
  340. <p>If an exception is thrown in <em class="replaceable"><code>body</code></em>, the first <em class="replaceable"><code>handler</code></em>
  341. is selected such that the thrown exception is an instance of
  342. the <em class="replaceable"><code>handler</code></em>’s <em class="replaceable"><code>type</code></em>. If no <em class="replaceable"><code>handler</code></em> is selected,
  343. the exception is propagated through the dynamic execution context
  344. until a matching <em class="replaceable"><code>handler</code></em> is found. (If no matching <em class="replaceable"><code>handler</code></em>
  345. is found, then an error message is printed, and the computation terminated.)
  346. </p>
  347. <p>Once a <em class="replaceable"><code>handler</code></em> is selected,
  348. the <em class="replaceable"><code>var</code></em> is bound to the thrown exception, and the <em class="replaceable"><code>exp</code></em> in
  349. the <em class="replaceable"><code>handler</code></em> are executed. The result of the <code class="literal">try-catch</code>
  350. is the result of <em class="replaceable"><code>body</code></em> if no exception is thrown, or the
  351. value of the last <em class="replaceable"><code>exp</code></em> in the selected <em class="replaceable"><code>handler</code></em> if an
  352. exception is thrown.
  353. </p>
  354. <p>(This is implemented just like Java’s <code class="literal">try</code>-<code class="literal">catch</code>.)
  355. </p>
  356. </blockquote>
  357. </div>
  358. </section>
  359. </section>
  360. <footer>
  361. <div class="navfooter">
  362. <ul>
  363. <li>
  364. <b class="toc">
  365. <a href="Exceptions.xhtml#idm139667877898080">Simple error objects</a>
  366. </b>
  367. </li>
  368. <li>
  369. <b class="toc">
  370. <a href="Exceptions.xhtml#idm139667877874016">Named exceptions</a>
  371. </b>
  372. </li>
  373. <li>
  374. <b class="toc">
  375. <a href="Exceptions.xhtml#idm139667877853776">Native exception handling</a>
  376. </b>
  377. </li>
  378. </ul>
  379. <p>
  380. Up: <a accesskey="u" href="Program-structure.xhtml">Program structure</a></p>
  381. <p>
  382. Previous: <a accesskey="p" href="Threads.xhtml">Threads</a></p>
  383. </div>
  384. </footer>
  385. </body>
  386. </html>