01: package kawa.lang;
02:
03: import gnu.mapping.*;
04:
05: /** Used to implement catch/throw named handlers as in Guile:
06: * (catch 'key (lambda () ... (throw 'key ARGS ...) ...)
07: * (lambda (KEY ARGS ...) HANDLER))
08: */
09:
10: public class NamedException extends RuntimeException {
11: String name;
12: // Arguments to throw (including, for efficiency, the name).
13: Object[] args;
14:
15: public NamedException(String name, Object[] args) {
16: this .name = name;
17: this .args = args;
18: }
19:
20: public void checkMatch(Object key) {
21: if (key != this .name && key != Boolean.TRUE)
22: throw this ;
23: }
24:
25: public Object applyHandler(Object key, Procedure handler)
26: throws Throwable {
27: checkMatch(key);
28: return handler.applyN(args);
29: }
30:
31: public String toString() {
32: StringBuffer buf = new StringBuffer();
33: buf.append("#<ERROR");
34: int i = 0;
35: int len = args.length;
36: // Skip initial 'misc-error as generated by the error procedure.
37: if (len > 1 && args[0] == "misc-error")
38: i++;
39: for (; i < len; i++) {
40: buf.append(' ');
41: buf.append(args[i]);
42: }
43: buf.append(">");
44: return buf.toString();
45: }
46: }
|