01: /*
02: * ThrowCmd.java --
03: *
04: * Implements the java::throw command.
05: *
06: * Copyright (c) 1997 Sun Microsystems, Inc.
07: *
08: * See the file "license.terms" for information on usage and
09: * redistribution of this file, and for a DISCLAIMER OF ALL
10: * WARRANTIES.
11: *
12: * RCS: @(#) $Id: JavaThrowCmd.java,v 1.2 2002/12/30 06:28:06 mdejong Exp $
13: */
14:
15: package tcl.lang;
16:
17: import java.lang.reflect.*;
18: import java.beans.*;
19:
20: /*
21: * This class implements the built-in "java::throw" command in Tcl.
22: */
23:
24: class JavaThrowCmd implements Command {
25:
26: /*
27: *----------------------------------------------------------------------
28: *
29: * cmdProc --
30: *
31: * This procedure is invoked as part of the Command interface to
32: * process the "java::throw" Tcl command. It receives a Throwable
33: * object and throws it by encapsulating the Throwable inside
34: * a ReflectException, which inherits from TclException. If the
35: * Throwable is already of type TclException, throw it after
36: * resetting the interp result to the TclException message.
37: *
38: * Results:
39: * None.
40: *
41: * Side effects:
42: * Can change the interp result, errorInfo, and errorCode.
43: *
44: *----------------------------------------------------------------------
45: */
46:
47: public void cmdProc(Interp interp, // Current interpreter.
48: TclObject argv[]) // Argument list.
49: throws TclException // Standard Tcl exception.
50: {
51: if (argv.length != 2) {
52: throw new TclNumArgsException(interp, 1, argv,
53: "throwableObj");
54: }
55:
56: Object javaObj = null;
57: javaObj = ReflectObject.get(interp, argv[1]);
58:
59: if (!(javaObj instanceof Throwable)) {
60: throw new TclException(interp,
61: "bad object: must be an instance of Throwable");
62: } else if (javaObj instanceof TclException) {
63: TclException te = (TclException) javaObj;
64: interp.setResult(te.getMessage());
65: throw te;
66: } else {
67: throw new ReflectException(interp, (Throwable) javaObj);
68: }
69: }
70:
71: } // end JavaThrowCmd
|