2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / EcmaError.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  *\r
3  * The contents of this file are subject to the Netscape Public\r
4  * License Version 1.1 (the "License"); you may not use this file\r
5  * except in compliance with the License. You may obtain a copy of\r
6  * the License at http://www.mozilla.org/NPL/\r
7  *\r
8  * Software distributed under the License is distributed on an "AS\r
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
10  * implied. See the License for the specific language governing\r
11  * rights and limitations under the License.\r
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\r
16  * The Initial Developer of the Original Code is Netscape\r
17  * Communications Corporation.  Portions created by Netscape are\r
18  * Copyright (C) 1997-1999 Netscape Communications Corporation. All\r
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s): \r
22  * Roger Lawrence\r
23  * \r
24  * Alternatively, the contents of this file may be used under the\r
25  * terms of the GNU Public License (the "GPL"), in which case the\r
26  * provisions of the GPL are applicable instead of those above.\r
27  * If you wish to allow use of your version of this file only\r
28  * under the terms of the GPL and not to allow others to use your\r
29  * version of this file under the NPL, indicate your decision by\r
30  * deleting the provisions above and replace them with the notice\r
31  * and other provisions required by the GPL.  If you do not delete\r
32  * the provisions above, a recipient may use your version of this\r
33  * file under either the NPL or the GPL.\r
34  */\r
35 \r
36 // API class\r
37 \r
38 package org.mozilla.javascript;\r
39 \r
40 /**\r
41  * The class of exceptions raised by the engine as described in \r
42  * ECMA edition 3. See section 15.11.6 in particular.\r
43  */\r
44 public class EcmaError extends RuntimeException {\r
45 \r
46     /**\r
47      * Create an exception with the specified detail message.\r
48      *\r
49      * Errors internal to the JavaScript engine will simply throw a\r
50      * RuntimeException.\r
51      *\r
52      * @param nativeError the NativeError object constructed for this error\r
53      * @param sourceName the name of the source reponsible for the error\r
54      * @param lineNumber the line number of the source\r
55      * @param columnNumber the columnNumber of the source (may be zero if\r
56      *                     unknown)\r
57      * @param lineSource the source of the line containing the error (may be \r
58      *                   null if unknown)\r
59      */\r
60     public EcmaError(NativeError nativeError, String sourceName, \r
61                      int lineNumber, int columnNumber, String lineSource) \r
62     {\r
63         super("EcmaError");\r
64         errorObject = nativeError;\r
65         this.sourceName = sourceName;\r
66         this.lineNumber = lineNumber;\r
67         this.columnNumber = columnNumber;\r
68         this.lineSource = lineSource;\r
69     }\r
70     \r
71     /**\r
72      * Return a string representation of the error, which currently consists \r
73      * of the name of the error together with the message.\r
74      */\r
75     public String toString() {\r
76         if (sourceName != null && lineNumber > 0)\r
77             return errorObject.toString() + " (" + sourceName + \r
78                "; line " + lineNumber + ")";\r
79         else\r
80             return errorObject.toString();\r
81     }\r
82     \r
83     /**\r
84      * Gets the name of the error.\r
85      * \r
86      * ECMA edition 3 defines the following\r
87      * errors: EvalError, RangeError, ReferenceError, \r
88      * SyntaxError, TypeError, and URIError. Additional error names\r
89      * may be added in the future.\r
90      * \r
91      * See ECMA edition 3, 15.11.7.9.\r
92      * \r
93      * @return the name of the error. \r
94      */\r
95     public String getName() {\r
96         return errorObject.getName();\r
97     }\r
98     \r
99     /**\r
100      * Gets the message corresponding to the error.\r
101      * \r
102      * See ECMA edition 3, 15.11.7.10.\r
103      * \r
104      * @return an implemenation-defined string describing the error.\r
105      */\r
106     public String getMessage() {\r
107         return errorObject.getMessage();\r
108     }\r
109     \r
110     /**\r
111      * Get the name of the source containing the error, or null\r
112      * if that information is not available.\r
113      */\r
114     public String getSourceName() {\r
115         return sourceName;\r
116     }\r
117     \r
118     /**\r
119      * Returns the line number of the statement causing the error,\r
120      * or zero if not available.\r
121      */\r
122     public int getLineNumber() {\r
123         return lineNumber;\r
124     }\r
125     \r
126     /**\r
127      * Get the error object corresponding to this exception.\r
128      */\r
129     public Scriptable getErrorObject() {\r
130         return errorObject;\r
131     }\r
132     \r
133     /**\r
134      * The column number of the location of the error, or zero if unknown.\r
135      */\r
136     public int getColumnNumber() {\r
137         return columnNumber;\r
138     }\r
139     \r
140     /**\r
141      * The source of the line causing the error, or zero if unknown.\r
142      */\r
143     public String getLineSource() {\r
144         return lineSource;\r
145     }\r
146     \r
147     private NativeError errorObject;\r
148     private String sourceName;\r
149     private int lineNumber;\r
150     private int columnNumber;\r
151     private String lineSource;\r
152 }\r