01: /*
02: * TclNumArgsException.java
03: *
04: * Copyright (c) 1997 Sun Microsystems, Inc.
05: *
06: * See the file "license.terms" for information on usage and
07: * redistribution of this file, and for a DISCLAIMER OF ALL
08: * WARRANTIES.
09: *
10: * RCS: @(#) $Id: TclNumArgsException.java,v 1.3 2003/01/12 02:44:28 mdejong Exp $
11: *
12: */
13:
14: package tcl.lang;
15:
16: /**
17: * This exception is used to report wrong number of arguments in Tcl scripts.
18: */
19:
20: public class TclNumArgsException extends TclException {
21:
22: /**
23: * Creates a TclException with the appropiate Tcl error
24: * message for having the wring number of arguments to a Tcl command.
25: * <p>
26: * Example: <pre>
27: *
28: * if (argv.length != 3) {
29: * throw new TclNumArgsException(interp, 1, argv, "option name");
30: * }
31: * </pre>
32: *
33: * @param interp current Interpreter.
34: * @param argc the number of arguments to copy from the offending
35: * command to put into the error message.
36: * @param argv the arguments of the offending command.
37: * @param message extra message to appear in the error message that
38: * explains the proper usage of the command.
39: * @exception TclException is always thrown.
40: */
41:
42: public TclNumArgsException(Interp interp, int argc,
43: TclObject argv[], String message) throws TclException {
44: super (TCL.ERROR);
45:
46: if (interp != null) {
47: StringBuffer buff = new StringBuffer(50);
48: buff.append("wrong # args: should be \"");
49:
50: for (int i = 0; i < argc; i++) {
51: if (argv[i].getInternalRep() instanceof TclIndex) {
52: buff.append(argv[i].getInternalRep().toString());
53: } else {
54: buff.append(argv[i].toString());
55: }
56: if (i < (argc - 1)) {
57: buff.append(" ");
58: }
59: }
60: if ((message != null) && (message.length() != 0)) {
61: buff.append(" " + message);
62: }
63: buff.append("\"");
64: interp.setResult(buff.toString());
65: }
66: }
67: }
|