KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > script > ScriptException


1 /*
2  * @(#)ScriptException.java 1.2 05/11/17 14:24:14
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
6  */

7
8 package javax.script;
9
10 /**
11  * The generic <code>Exception</code> class for the Scripting APIs. Checked
12  * exception types thrown by underlying scripting implementations must be wrapped in instances of
13  * <code>ScriptException</code>. The class has members to store line and column numbers and
14  * filenames if this information is available.
15  *
16  * @author Mike Grogan
17  * @version 1.0
18  * @since 1.6
19  */

20 public class ScriptException extends Exception JavaDoc {
21     
22     private String JavaDoc fileName;
23     private int lineNumber;
24     private int columnNumber;
25     
26     /**
27      * Creates a <code>ScriptException</code> with a String to be used in its message.
28      * Filename, and line and column numbers are unspecified.
29      *
30      * @param s The String to use in the message.
31      */

32     public ScriptException(String JavaDoc s) {
33         super(s);
34         fileName = null;
35         lineNumber = -1;
36         columnNumber = -1;
37     }
38     
39     /**
40      * Creates a <code>ScriptException</code> wrapping an <code>Exception</code> thrown by an underlying
41      * interpreter. Line and column numbers and filename are unspecified.
42      *
43      * @param e The wrapped <code>Exception</code>.
44      */

45     public ScriptException(Exception JavaDoc e) {
46         super(e);
47         fileName = null;
48         lineNumber = -1;
49         columnNumber = -1;
50     }
51     
52     /**
53      * Creates a <code>ScriptException</code> with message, filename and linenumber to
54      * be used in error messages.
55      *
56      * @param message The string to use in the message
57      *
58      * @param fileName The file or resource name describing the location of a script error
59      * causing the <code>ScriptException</code> to be thrown.
60      *
61      * @param lineNumber A line number describing the location of a script error causing
62      * the <code>ScriptException</code> to be thrown.
63      */

64     public ScriptException(String JavaDoc message, String JavaDoc fileName, int lineNumber) {
65         super(message);
66         this.fileName = fileName;
67         this.lineNumber = lineNumber;
68         this.columnNumber = -1;
69     }
70     
71     /**
72      * <code>ScriptException</code> constructor specifying message, filename, line number
73      * and column number.
74      * @param message The message.
75      * @param fileName The filename
76      * @param lineNumber the line number.
77      * @param columnNumber the column number.
78      */

79     public ScriptException(String JavaDoc message,
80             String JavaDoc fileName,
81             int lineNumber,
82             int columnNumber) {
83         super(message);
84         this.fileName = fileName;
85         this.lineNumber = lineNumber;
86         this.columnNumber = columnNumber;
87     }
88     
89     /**
90      * Returns a message containing the String passed to a constructor as well as
91      * line and column numbers and filename if any of these are known.
92      * @return The error message.
93      */

94     public String JavaDoc getMessage() {
95         String JavaDoc ret = super.getMessage();
96         if (fileName != null) {
97             ret += (" in " + fileName);
98             if (lineNumber != -1) {
99                 ret += " at line number " + lineNumber;
100             }
101             
102             if (columnNumber != -1) {
103                 ret += " at column number " + columnNumber;
104             }
105         }
106         
107         return ret;
108     }
109     
110     /**
111      * Get the line number on which an error occurred.
112      * @return The line number. Returns -1 if a line number is unavailable.
113      */

114     public int getLineNumber() {
115         return lineNumber;
116     }
117     
118     /**
119      * Get the column number on which an error occurred.
120      * @return The column number. Returns -1 if a column number is unavailable.
121      */

122     public int getColumnNumber() {
123         return columnNumber;
124     }
125     
126     /**
127      * Get the source of the script causing the error.
128      * @return The file name of the script or some other string describing the script
129      * source. May return some implementation-defined string such as <i>&lt;unknown&gt;</i>
130      * if a description of the source is unavailable.
131      */

132     public String JavaDoc getFileName() {
133         return fileName;
134     }
135 }
136
Popular Tags