source: project/wiki/srfi-34 @ 7314

Last change on this file since 7314 was 7314, checked in by svnwiki, 13 years ago

Changes applied for ben (12.15.146.254) through svnwiki:

File size: 7.1 KB
Line 
1=== Description
2
3SRFI-34 Exception Handling for Programs
4
5=== Examples
6
7Examples are in examples/srfi-34-examples.scm.
8
9<enscript highlight="scheme">
10;;;
11;;; (raise OBJ)
12;;;
13
14(require-extension srfi-34)
15
16(raise 'heck)
17
18;;; The default exception handler calls error aborting the script.
19</enscript>
20
21<enscript highlight="scheme">
22;;;
23;;; (guard (VAR CLAUSE1 CLAUSE2 ...) BODY)
24;;;
25
26;;; Use guard to wrap code that might raise an exception so that the default
27;;; handler doesn't abort the program
28
29(require-extension srfi-34)
30
31;;; This will print:
32;;;   Doing some stuff
33;;;   Caught an unidentified flying exception: bizarre-exception
34;;; and then it will return the 'bizarre-exception
35(guard ( e
36       [(eq? e 'weird-exception)
37        (display "Caught a weird-exception")(newline)]
38       [(eq? e 'odd-exception)
39        (display "Caught an odd-exception")(newline)]
40       [else
41        (display "Caught an unidentified flying exception: ")
42        (write e)(newline)
43        e])
44       (display "Doing some stuff")(newline)
45       (raise 'bizarre-exception)
46       (display "Won't get here")(newline) )
47</enscript>
48
49<enscript highlight="scheme">
50;;;
51;;; The => syntax for guard: Example 1
52;;;
53(require-extension srfi-34)
54
55;;; This will return: 42
56(guard (condition
57         ((assq 'a condition) => cdr)
58         ((assq 'b condition)))
59  (raise (list (cons 'a 42))))
60</enscript>
61
62<enscript highlight="scheme">
63;;;
64;;; The => syntax for guard: Example 2
65;;;
66(require-extension srfi-34)
67
68;;; This will return: (b . 23)
69(guard (condition
70         ((assq 'a condition) => cdr)
71         ((assq 'b condition)))
72  (raise (list (cons 'b 23))))
73</enscript>
74
75<enscript highlight="scheme">
76;;;
77;;; ( with-exception-handler HANDLER THUNK )
78;;;
79
80;;; This example demonstrates a lower level procedure that I don't think would
81;;; ordinarily be used.  The guard macro takes care of the continuation
82;;; plumbing you see here for you, ultimately doing something like this.
83;;; There is also a similar 'with-exception-handlers' documented in the srfi.
84
85(require-extension srfi-34)
86
87(call-with-current-continuation
88  ;;; We want control to resume at k if an exception is raised, not
89  ;;; to continue normally at the next line after the call to raise.
90  ;;; ( raise is smart enough call error if it finds that the exception
91  ;;; handler returns control to it instead of to another continuation as
92  ;;; it should )
93  (lambda (k)
94    (with-exception-handler
95      ;;; This is the exception handler that gets stored in
96      ;;; in the dynamic environment. Ultimately, it passes control to
97      ;;; k because returning is an error
98      (lambda (e)
99        (display "I handled an exception: ")(write e)(newline)
100        (display "Passing it to previous continuation")(newline)
101        (k e))
102      ;;; This is the thunk that may throw an exception
103      (lambda ()
104        (display "Doing more stuff")(newline)
105        (raise 'yet-another-exception)
106        (display "Won't get here")(newline)))) )
107
108</enscript>
109=== Author
110
111SRFI-34 Reference Implementation Authors: Richard Kelsey, Michael Sperber.
112
113Packaged as an egg by [[Ben Clark]]
114=== License
115
116Derived (almost verbatim) from the reference implementation of srfi-34. The copyright on that code is:
117
118<blockquote>
119Copyright (C) Richard Kelsey, Michael Sperber (2002). All Rights Reserved.
120
121Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
122
123The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
124
125THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
126</blockquote>
127
128Modifications are copyright [[Ben Clark]] (2007), governed by the same conditions. Any errors are mine.
129
130=== Requires
131
132* syntax-case
133=== Documentation
134
135NOTE: Chicken implements SFRI-12, a withdrawn ( but more featureful ) exception system.  If you don't need SFRI-34 for portablility, there is no reason to use this egg over the SFRI-12 functionality chicken provides out of the box. 
136
137srfi-34.egg contains the reference implementation of srfi-34 packaged as a chicken egg.
138
139See http://srfi.schemers.org/srfi-34/srfi-34.html for additional documentation
140
141The module maintains a list of exception handlers using dynamic-wind. This list begins it's life defined thusly:
142
143<enscript highlight="scheme">
144(define *current-exception-handlers*
145  (list (lambda (condition)
146          (error "unhandled exception" condition))))
147</enscript>
148
149A naked call to '''raise''' would end up calling the default handler procedure contained in the *current-exception-handlers* list, aborting the program.
150
151----
152
153'''procedure: (raise OBJ)'''
154
155Raise an exception. OBJ can be any scheme object. Invokes the 'current exception handler' thunk, which is the car of the *current-exception-handlers* list. This handler thunk handles the exception OBJ.
156
157The handler thunk should then either call error to stop program execution, or it should have stored the continuation at which to resume execution of the program after handling the exception, and pass it's result to that continuation where the program's flow of execution will resume.
158
159Installing an exception handler that returns will cause the raise procedure to abort the program by call error, alerting that the exception handler thunk erroneously returned control to the raise procedure instead of to a different continuation as it should.
160
161----
162
163'''macro: (guard (VAR CLAUSE1 CLAUSE2 ...) BODY )'''
164
165Syntax: Each clause should have the same form as a cond clause.
166
167Semantics: Whereas raise is analogous to the throw commonly found in other languages, guard is analogous to the try/catch syntax often implemented. VAR gets bound to the exception thrown by BODY, and then the CLAUSEs which have access to VAR, are evaluated until one of the tests succeeds, and the corresponding code gets run. As with cond, the else test always succeeds and so can be used to handle general exceptions.
168
169Notes: guard also supports '''<nowiki>=&gt;</nowiki>''' in the clauses. If the test returns a useful value, then '''<nowiki>=&gt;</nowiki>''' can be used to specify a procedure to apply to that value in that case. See the examples section and the srfi for clarification on this.
170
171----
172
173'''procedure: (with-exception-handler HANDLER THUNK)'''
174
175Returns the result(s) of invoking thunk. Handler must be a procedure that accepts one argument. It is installed as the current exception handler for the dynamic extent (as determined by dynamic-wind) of the invocation of thunk.
176
177----
178=== Version
179
180* 0.1 Initial release
Note: See TracBrowser for help on using the repository browser.