001: /*
002: * %W% %E% %U%
003: *
004: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
005: * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
006: */
007:
008: package javax.script;
009:
010: /**
011: * The generic <code>Exception</code> class for the Scripting APIs. Checked
012: * exception types thrown by underlying scripting implementations must be wrapped in instances of
013: * <code>ScriptException</code>. The class has members to store line and column numbers and
014: * filenames if this information is available.
015: *
016: * @author Mike Grogan
017: * @version 1.0
018: * @since 1.6
019: */
020: public class ScriptException extends Exception {
021:
022: private String fileName;
023: private int lineNumber;
024: private int columnNumber;
025:
026: /**
027: * Creates a <code>ScriptException</code> with a String to be used in its message.
028: * Filename, and line and column numbers are unspecified.
029: *
030: * @param s The String to use in the message.
031: */
032: public ScriptException(String s) {
033: super (s);
034: fileName = null;
035: lineNumber = -1;
036: columnNumber = -1;
037: }
038:
039: /**
040: * Creates a <code>ScriptException</code> wrapping an <code>Exception</code> thrown by an underlying
041: * interpreter. Line and column numbers and filename are unspecified.
042: *
043: * @param e The wrapped <code>Exception</code>.
044: */
045: public ScriptException(Exception e) {
046: super (e);
047: fileName = null;
048: lineNumber = -1;
049: columnNumber = -1;
050: }
051:
052: /**
053: * Creates a <code>ScriptException</code> with message, filename and linenumber to
054: * be used in error messages.
055: *
056: * @param message The string to use in the message
057: *
058: * @param fileName The file or resource name describing the location of a script error
059: * causing the <code>ScriptException</code> to be thrown.
060: *
061: * @param lineNumber A line number describing the location of a script error causing
062: * the <code>ScriptException</code> to be thrown.
063: */
064: public ScriptException(String message, String fileName,
065: int lineNumber) {
066: super (message);
067: this .fileName = fileName;
068: this .lineNumber = lineNumber;
069: this .columnNumber = -1;
070: }
071:
072: /**
073: * <code>ScriptException</code> constructor specifying message, filename, line number
074: * and column number.
075: * @param message The message.
076: * @param fileName The filename
077: * @param lineNumber the line number.
078: * @param columnNumber the column number.
079: */
080: public ScriptException(String message, String fileName,
081: int lineNumber, int columnNumber) {
082: super (message);
083: this .fileName = fileName;
084: this .lineNumber = lineNumber;
085: this .columnNumber = columnNumber;
086: }
087:
088: /**
089: * Returns a message containing the String passed to a constructor as well as
090: * line and column numbers and filename if any of these are known.
091: * @return The error message.
092: */
093: public String getMessage() {
094: String ret = super .getMessage();
095: if (fileName != null) {
096: ret += (" in " + fileName);
097: if (lineNumber != -1) {
098: ret += " at line number " + lineNumber;
099: }
100:
101: if (columnNumber != -1) {
102: ret += " at column number " + columnNumber;
103: }
104: }
105:
106: return ret;
107: }
108:
109: /**
110: * Get the line number on which an error occurred.
111: * @return The line number. Returns -1 if a line number is unavailable.
112: */
113: public int getLineNumber() {
114: return lineNumber;
115: }
116:
117: /**
118: * Get the column number on which an error occurred.
119: * @return The column number. Returns -1 if a column number is unavailable.
120: */
121: public int getColumnNumber() {
122: return columnNumber;
123: }
124:
125: /**
126: * Get the source of the script causing the error.
127: * @return The file name of the script or some other string describing the script
128: * source. May return some implementation-defined string such as <i><unknown></i>
129: * if a description of the source is unavailable.
130: */
131: public String getFileName() {
132: return fileName;
133: }
134: }
|