Debian Codegraph @

rhino/src/org/mozilla/javascript/JavaScriptException.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // API class
8
9 package org.mozilla.javascript;
10
11 /**
12 * Java reflection of JavaScript exceptions.
13 * Instances of this class are thrown by the JavaScript 'throw' keyword.
14 *
15 * @author Mike McCabe
16 */
17 public class JavaScriptException extends RhinoException
18 {
19 static final long serialVersionUID = -7666130513694669293L;
20
21 /**
22 * @deprecated
23 * Use {@link WrappedException#WrappedException(Throwable)} to report
24 * exceptions in Java code.
25 */
26 @Deprecated
27 public JavaScriptException(Object value)
28 {
29 this(value, "", 0);
30 }
31
32 /**
33 * Create a JavaScript exception wrapping the given JavaScript value
34 *
35 * @param value the JavaScript value thrown.
36 */
37 public JavaScriptException(Object value, String sourceName, int lineNumber)
38 {
39 recordErrorOrigin(sourceName, lineNumber, null, 0);
40 this.value = value;
41 // Fill in fileName and lineNumber automatically when not specified
42 // explicitly, see Bugzilla issue #342807
43 if (value instanceof NativeError && Context.getContext()
44 .hasFeature(Context.FEATURE_LOCATION_INFORMATION_IN_ERROR)) {
45 NativeError error = (NativeError) value;
46 if (!error.has("fileName", error)) {
47 error.put("fileName", error, sourceName);
48 }
49 if (!error.has("lineNumber", error)) {
50 error.put("lineNumber", error, Integer.valueOf(lineNumber));
51 }
52 // set stack property, see bug #549604
53 error.setStackProvider(this);
54 }
55 }
56
57 @Override
58 public String details()
59 {
60 if (value == null) {
61 return "null";
62 } else if (value instanceof NativeError) {
63 return value.toString();
64 }
65 try {
66 return ScriptRuntime.toString(value);
67 } catch (RuntimeException rte) {
68 // ScriptRuntime.toString may throw a RuntimeException
69 if (value instanceof Scriptable) {
70 return ScriptRuntime.defaultObjectToString((Scriptable)value);
71 } else {
72 return value.toString();
73 }
74 }
75 }
76
77 /**
78 * @return the value wrapped by this exception
79 */
80 public Object getValue()
81 {
82 return value;
83 }
84
85 /**
86 * @deprecated Use {@link RhinoException#sourceName()} from the super class.
87 */
88 @Deprecated
89 public String getSourceName()
90 {
91 return sourceName();
92 }
93
94 /**
95 * @deprecated Use {@link RhinoException#lineNumber()} from the super class.
96 */
97 @Deprecated
98 public int getLineNumber()
99 {
100 return lineNumber();
101 }
102
103 private Object value;
104 }