1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
\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
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
13 * The Original Code is Rhino code, released
\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
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
38 package org.mozilla.javascript;
\r
40 import java.lang.reflect.InvocationTargetException;
\r
43 * Java reflection of JavaScript exceptions. (Possibly wrapping a Java exception.)
\r
45 * @author Mike McCabe
\r
47 public class JavaScriptException extends Exception {
\r
50 public String sourceFile;
\r
53 * Create a JavaScript exception wrapping the given JavaScript value.
\r
55 * Instances of this class are thrown by the JavaScript 'throw' keyword.
\r
57 * @param value the JavaScript value thrown.
\r
59 public JavaScriptException(Object value) {
\r
60 super(ScriptRuntime.toString(value));
\r
61 line = Context.enter().interpreterLine;
\r
62 sourceFile = Context.enter().interpreterSourceFile;
\r
67 * Get the exception message.
\r
69 * <p>Will just convert the wrapped exception to a string.
\r
71 public String getMessage() {
\r
72 return ScriptRuntime.toString(value);
\r
75 static JavaScriptException wrapException(Scriptable scope,
\r
78 if (exn instanceof InvocationTargetException)
\r
79 exn = ((InvocationTargetException)exn).getTargetException();
\r
80 if (exn instanceof JavaScriptException)
\r
81 return (JavaScriptException)exn;
\r
82 Object wrapper = NativeJavaObject.wrap(scope, exn, Throwable.class);
\r
83 return new JavaScriptException(wrapper);
\r
87 * Get the exception value originally thrown. This may be a
\r
88 * JavaScript value (null, undefined, Boolean, Number, String,
\r
89 * Scriptable or Function) or a Java exception value thrown from a
\r
90 * host object or from Java called through LiveConnect.
\r
92 * @return the value wrapped by this exception
\r
94 public Object getValue() {
\r
95 if (value != null && value instanceof Wrapper)
\r
96 // this will also catch NativeStrings...
\r
97 return ((Wrapper)value).unwrap();
\r
103 * The JavaScript exception value. This value is not
\r
104 * intended for general use; if the JavaScriptException wraps a
\r
105 * Java exception, getScriptableValue may return a Scriptable
\r
106 * wrapping the original Java exception object.
\r
108 * We would prefer to go through a getter to encapsulate the value,
\r
109 * however that causes the bizarre error "nanosecond timeout value
\r
110 * out of range" on the MS JVM.
\r