source: project/wiki/srfi-34 @ 7250

Last change on this file since 7250 was 7250, checked in by svnwiki, 12 years ago

Changes applied for ben (12.15.146.254) through svnwiki:

File size: 6.9 KB
Line 
1== srfi-34
2
3[Picture of an egg]
4
5=== Description
6
7SRFI-34 Exception Handling for Programs
8
9=== Examples
10
11Examples are in examples/srfi-34-examples.scm.
12
13<enscript highlight="scheme">
14;;;
15;;; (raise OBJ)
16;;;
17
18(require-extension srfi-34)
19
20(raise 'heck)
21
22;;; The default exception handler calls error aborting the script.
23</enscript>
24
25<enscript highlight="scheme">
26;;;
27;;; (guard (VAR CLAUSE1 CLAUSE2 ...) BODY)
28;;;
29
30;;; Use guard to wrap code that might raise an exception so that the default
31;;; handler doesn't abort the program
32
33(require-extension srfi-34)
34
35;;; This will print:
36;;;   Doing some stuff
37;;;   Caught an unidentified flying exception: bizarre-exception
38;;; and then it will return the 'bizarre-exception
39(guard ( e
40       [(eq? e 'weird-exception)
41        (display "Caught a weird-exception")(newline)]
42       [(eq? e 'odd-exception)
43        (display "Caught an odd-exception")(newline)]
44       [else
45        (display "Caught an unidentified flying exception: ")
46        (write e)(newline)
47        e])
48       (display "Doing some stuff")(newline)
49       (raise 'bizarre-exception)
50       (display "Won't get here")(newline) )
51</enscript>
52
53<enscript highlight="scheme">
54;;;
55;;; The => syntax for guard: Example 1
56;;;
57(require-extension srfi-34)
58
59;;; This will return: 42
60(guard (condition
61         ((assq 'a condition) => cdr)
62         ((assq 'b condition)))
63  (raise (list (cons 'a 42))))
64</enscript>
65
66<enscript highlight="scheme">
67;;;
68;;; The => syntax for guard: Example 2
69;;;
70(require-extension srfi-34)
71
72;;; This will return: (b . 23)
73(guard (condition
74         ((assq 'a condition) => cdr)
75         ((assq 'b condition)))
76  (raise (list (cons 'b 23))))
77</enscript>
78
79<enscript highlight="scheme">
80;;;
81;;; ( with-exception-handler HANDLER THUNK )
82;;;
83
84;;; This example demonstrates a lower level procedure that I don't think would
85;;; ordinarily be used.  The guard macro takes care of the continuation
86;;; plumbing you see here for you, ultimately doing something like this.
87;;; There is also a similar 'with-exception-handlers' documented in the srfi.
88
89(require-extension srfi-34)
90
91(call-with-current-continuation
92  ;;; We want control to resume at k if an exception is raised, not
93  ;;; to continue normally at the next line after the call to raise.
94  ;;; ( raise is smart enough call error if it finds that the exception
95  ;;; handler returns control to it instead of to another continuation as
96  ;;; it should )
97  (lambda (k)
98    (with-exception-handler
99      ;;; This is the exception handler that gets stored in
100      ;;; in the dynamic environment. Ultimately, it passes control to
101      ;;; k because returning is an error
102      (lambda (e)
103        (display "I handled an exception: ")(write e)(newline)
104        (display "Passing it to previous continuation")(newline)
105        (k e))
106      ;;; This is the thunk that may throw an exception
107      (lambda ()
108        (display "Doing more stuff")(newline)
109        (raise 'yet-another-exception)
110        (display "Won't get here")(newline)))) )
111
112</enscript>
113=== Author
114
115SRFI-34 Reference Implementation Authors: Richard Kelsey, Michael Sperber.
116
117Packaged as an egg by Ben Clark
118
119=== License
120
121Derived (almost verbatim) from the reference implementation of srfi-34. The copyright on that code is:
122
123Copyright (C) Richard Kelsey, Michael Sperber (2002). All Rights Reserved.
124
125Permission 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:
126
127The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
128
129THE 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.
130
131Modifications are copyright Ben Clark (2007), governed by the same conditions. Any errors are mine.
132
133=== Requires
134
135* syntax-case
136
137=== Documentation
138
139srfi-34.egg contains the reference implementation of srfi-34 packaged as a chicken egg.
140
141See http://srfi.schemers.org/srfi-34/srfi-34.html for additional documentation
142
143The module maintains a list of exception handlers using dynamic-wind. It defines the default exception handler which aborts execution of the whole program, thusly:
144
145<enscript highlight="scheme">
146(define *current-exception-handlers*
147  (list (lambda (condition)
148          (error "unhandled exception" condition))))
149</enscript>
150
151A naked call to '''raise''' would end up calling this handler, aborting the program.
152
153----
154
155'''procedure: (raise OBJ)'''
156
157Raise 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.
158
159The 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.
160
161Installing 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.
162
163----
164
165'''macro: (guard (VAR CLAUSE1 CLAUSE2 ...) BODY )'''
166
167Syntax: Each clause should have the same form as a cond clause.
168
169Semantics: 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.
170
171Notes: 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.
172
173----
174
175'''procedure: (with-exception-handler HANDLER THUNK)'''
176
177Returns 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.
178
179----
180=== Version
181
182* 0.1 Initial release
Note: See TracBrowser for help on using the repository browser.