jdk/src/java.scripting/share/classes/javax/script/ScriptException.java
changeset 25859 3317bb8137f4
parent 14342 8435a30053c1
equal deleted inserted replaced
25858:836adbf7a2cd 25859:3317bb8137f4
       
     1 /*
       
     2  * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package javax.script;
       
    27 
       
    28 /**
       
    29  * The generic <code>Exception</code> class for the Scripting APIs.  Checked
       
    30  * exception types thrown by underlying scripting implementations must be wrapped in instances of
       
    31  * <code>ScriptException</code>.  The class has members to store line and column numbers and
       
    32  * filenames if this information is available.
       
    33  *
       
    34  * @author Mike Grogan
       
    35  * @since 1.6
       
    36  */
       
    37 public class ScriptException extends Exception {
       
    38 
       
    39     private static final long serialVersionUID = 8265071037049225001L;
       
    40 
       
    41     private String fileName;
       
    42     private int lineNumber;
       
    43     private int columnNumber;
       
    44 
       
    45     /**
       
    46      * Creates a <code>ScriptException</code> with a String to be used in its message.
       
    47      * Filename, and line and column numbers are unspecified.
       
    48      *
       
    49      * @param s The String to use in the message.
       
    50      */
       
    51     public ScriptException(String s) {
       
    52         super(s);
       
    53         fileName = null;
       
    54         lineNumber = -1;
       
    55         columnNumber = -1;
       
    56     }
       
    57 
       
    58     /**
       
    59      * Creates a <code>ScriptException</code> wrapping an <code>Exception</code> thrown by an underlying
       
    60      * interpreter.  Line and column numbers and filename are unspecified.
       
    61      *
       
    62      * @param e The wrapped <code>Exception</code>.
       
    63      */
       
    64     public ScriptException(Exception e) {
       
    65         super(e);
       
    66         fileName = null;
       
    67         lineNumber = -1;
       
    68         columnNumber = -1;
       
    69     }
       
    70 
       
    71     /**
       
    72      * Creates a <code>ScriptException</code> with message, filename and linenumber to
       
    73      * be used in error messages.
       
    74      *
       
    75      * @param message The string to use in the message
       
    76      *
       
    77      * @param fileName The file or resource name describing the location of a script error
       
    78      * causing the <code>ScriptException</code> to be thrown.
       
    79      *
       
    80      * @param lineNumber A line number describing the location of a script error causing
       
    81      * the <code>ScriptException</code> to be thrown.
       
    82      */
       
    83     public ScriptException(String message, String fileName, int lineNumber) {
       
    84         super(message);
       
    85         this.fileName = fileName;
       
    86         this.lineNumber = lineNumber;
       
    87         this.columnNumber = -1;
       
    88     }
       
    89 
       
    90     /**
       
    91      * <code>ScriptException</code> constructor specifying message, filename, line number
       
    92      * and column number.
       
    93      * @param message The message.
       
    94      * @param fileName The filename
       
    95      * @param lineNumber the line number.
       
    96      * @param columnNumber the column number.
       
    97      */
       
    98     public ScriptException(String message,
       
    99             String fileName,
       
   100             int lineNumber,
       
   101             int columnNumber) {
       
   102         super(message);
       
   103         this.fileName = fileName;
       
   104         this.lineNumber = lineNumber;
       
   105         this.columnNumber = columnNumber;
       
   106     }
       
   107 
       
   108     /**
       
   109      * Returns a message containing the String passed to a constructor as well as
       
   110      * line and column numbers and filename if any of these are known.
       
   111      * @return The error message.
       
   112      */
       
   113     public String getMessage() {
       
   114         String ret = super.getMessage();
       
   115         if (fileName != null) {
       
   116             ret += (" in " + fileName);
       
   117             if (lineNumber != -1) {
       
   118                 ret += " at line number " + lineNumber;
       
   119             }
       
   120 
       
   121             if (columnNumber != -1) {
       
   122                 ret += " at column number " + columnNumber;
       
   123             }
       
   124         }
       
   125 
       
   126         return ret;
       
   127     }
       
   128 
       
   129     /**
       
   130      * Get the line number on which an error occurred.
       
   131      * @return The line number.  Returns -1 if a line number is unavailable.
       
   132      */
       
   133     public int getLineNumber() {
       
   134         return lineNumber;
       
   135     }
       
   136 
       
   137     /**
       
   138      * Get the column number on which an error occurred.
       
   139      * @return The column number.  Returns -1 if a column number is unavailable.
       
   140      */
       
   141     public int getColumnNumber() {
       
   142         return columnNumber;
       
   143     }
       
   144 
       
   145     /**
       
   146      * Get the source of the script causing the error.
       
   147      * @return The file name of the script or some other string describing the script
       
   148      * source.  May return some implementation-defined string such as <i>&lt;unknown&gt;</i>
       
   149      * if a description of the source is unavailable.
       
   150      */
       
   151     public String getFileName() {
       
   152         return fileName;
       
   153     }
       
   154 }