Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Javascript stack traces

1,098 views
Skip to first unread message

Amit

unread,
Jan 29, 2008, 10:37:52 PM1/29/08
to
I am interfacing to some Java code using Javascript. Is it possible to
print just the javascript stack trace if an exception occurs?

My code looks like this:

try {

} catch (e) {

if (e instanceof java.lang.Exception)
e.printStackTrace();

}

'e.printStackTrace' calls the java.lang.Exception stack dump method.
In addition to this I would also like to display the javascript stack
dump when a Java exception is thrown. Is there a way to do it? Also is
there a way to print the combined Java and Javascript stack trace?

Regards
Amit


Attila Szegedi

unread,
Feb 2, 2008, 10:22:55 AM2/2/08
to Amit, dev-tech-js-...@lists.mozilla.org
"e" will not be a Java exception, even if you threw a Java exception.
However, Rhino has a non-ECMA extension to the error object in this
case on the error object - a property named "javaException".
Therefore, "e.javaException" gives you access to the underlying Java
exception.

In compiled mode, you should see source names and line numbers in
script (as JS is compiled to a Java class with appropriate line number
information).

In interpreted mode, RhinoException and its subclasses will feature
script locations in the stack trace. If you let your exception
propagate out of the Script.exec() and catch it in Java and log it, it
will almost always be a RhinoException, so you get the script stack
trace.

There is another non-ECMA extension to the error object - a property
named "rhinoException". When an exception is thrown in Rhino, it is
usually wrapped into a WrappedException (which is a subclass of
RhinoException). Calling printStackTrace on it might be more useful.

So,

try
{
}
catch(e)
{
if(e.rhinoException != null)
{
e.rhinoException.printStackTrace();
}
else if(e.javaException != null)
{
e. javaException.printStackTrace();
}
}

should work from script and produce script stack locations in the trace.

Attila.

However, you won't see a RhinoException

0 new messages