source: project/wiki/eggref/4/srfi-34 @ 22990

Last change on this file since 22990 was 22990, checked in by sjamaan, 9 years ago

Update a few wiki pages so they don't have a title that says 'description'

File size: 7.6 KB
Line 
1[[tags: egg]]
2
3== SRFI-34
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=== License
119
120Derived (almost verbatim) from the reference implementation of srfi-34. The copyright on that code is:
121
122<blockquote>
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</blockquote>
131
132Modifications are copyright [[Ben Clark]] (2007), governed by the same conditions. Any errors are mine.
133
134=== Documentation
135
136NOTE: Chicken implements SFRI-12, a withdrawn SRFI that is nonetheless a more featureful exception system than the one implemented in SRFI-34.  If you don't need SFRI-34 for portablility to some other scheme, there is no reason to use this egg over the SFRI-12 functionality chicken provides out of the box.
137
138NOTE: This egg has been updated to use Chicken's native handlers, so it should work as expected now.  What this boils down to: all this egg does is define a {{guard}} macro.  The exceptions system is provided by Chicken itself, and raise is provided by srfi-18.  This eggdoc is entirely OUT OF DATE.
139
140srfi-34.egg contains the reference implementation of srfi-34 packaged as a chicken egg.
141
142See http://srfi.schemers.org/srfi-34/srfi-34.html for additional documentation
143
144The module maintains a list of exception handlers using dynamic-wind. This list begins it's life defined thusly:
145
146<enscript highlight="scheme">
147(define *current-exception-handlers*
148  (list (lambda (condition)
149          (error "unhandled exception" condition))))
150</enscript>
151
152A naked call to '''raise''' would end up calling the default handler procedure contained in the *current-exception-handlers* list, aborting the program.
153
154----
155
156'''procedure: (raise OBJ)'''
157
158Raise 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.
159
160The 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.
161
162Installing 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.
163
164----
165
166'''macro: (guard (VAR CLAUSE1 CLAUSE2 ...) BODY )'''
167
168Syntax: Each clause should have the same form as a cond clause.
169
170Semantics: 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.
171
172Notes: 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.
173
174----
175
176'''procedure: (with-exception-handler HANDLER THUNK)'''
177
178Returns 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.
179
180----
181=== Version
182
183* 0.5 thread-safety fixes by Joerg Wittenberger [felix]
184* 0.4 setup-script fixes by Christian Kellermann
185* 0.3 Port to Chicken 4 [C-Keen]
186* 0.2 20080726 [elf]  Working release.
187* 0.1 Initial release
Note: See TracBrowser for help on using the repository browser.