Path: blob/master/src/java.scripting/share/classes/javax/script/ScriptException.java
41153 views
/*1* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.script;2627/**28* The generic <code>Exception</code> class for the Scripting APIs. Checked29* exception types thrown by underlying scripting implementations must be wrapped in instances of30* <code>ScriptException</code>. The class has members to store line and column numbers and31* filenames if this information is available.32*33* @author Mike Grogan34* @since 1.635*/36public class ScriptException extends Exception {3738private static final long serialVersionUID = 8265071037049225001L;3940private final String fileName;41private final int lineNumber;42private final int columnNumber;4344/**45* Creates a <code>ScriptException</code> with a String to be used in its message.46* Filename, and line and column numbers are unspecified.47*48* @param s The String to use in the message.49*/50public ScriptException(String s) {51super(s);52fileName = null;53lineNumber = -1;54columnNumber = -1;55}5657/**58* Creates a <code>ScriptException</code> wrapping an <code>Exception</code> thrown by an underlying59* interpreter. Line and column numbers and filename are unspecified.60*61* @param e The wrapped <code>Exception</code>.62*/63public ScriptException(Exception e) {64super(e);65fileName = null;66lineNumber = -1;67columnNumber = -1;68}6970/**71* Creates a <code>ScriptException</code> with message, filename and linenumber to72* be used in error messages.73*74* @param message The string to use in the message75*76* @param fileName The file or resource name describing the location of a script error77* causing the <code>ScriptException</code> to be thrown.78*79* @param lineNumber A line number describing the location of a script error causing80* the <code>ScriptException</code> to be thrown.81*/82public ScriptException(String message, String fileName, int lineNumber) {83super(message);84this.fileName = fileName;85this.lineNumber = lineNumber;86this.columnNumber = -1;87}8889/**90* <code>ScriptException</code> constructor specifying message, filename, line number91* and column number.92* @param message The message.93* @param fileName The filename94* @param lineNumber the line number.95* @param columnNumber the column number.96*/97public ScriptException(String message,98String fileName,99int lineNumber,100int columnNumber) {101super(message);102this.fileName = fileName;103this.lineNumber = lineNumber;104this.columnNumber = columnNumber;105}106107/**108* Returns a message containing the String passed to a constructor as well as109* line and column numbers and filename if any of these are known.110* @return The error message.111*/112public String getMessage() {113String ret = super.getMessage();114if (fileName != null) {115ret += (" in " + fileName);116if (lineNumber != -1) {117ret += " at line number " + lineNumber;118}119120if (columnNumber != -1) {121ret += " at column number " + columnNumber;122}123}124125return ret;126}127128/**129* Get the line number on which an error occurred.130* @return The line number. Returns -1 if a line number is unavailable.131*/132public int getLineNumber() {133return lineNumber;134}135136/**137* Get the column number on which an error occurred.138* @return The column number. Returns -1 if a column number is unavailable.139*/140public int getColumnNumber() {141return columnNumber;142}143144/**145* Get the source of the script causing the error.146* @return The file name of the script or some other string describing the script147* source. May return some implementation-defined string such as <i><unknown></i>148* if a description of the source is unavailable.149*/150public String getFileName() {151return fileName;152}153}154155156