try {
doSomething();
}
catch (MyError e) {
handleError()
}
In JavaScript I can write the more verbose
try {
doSomething();
}
catch (e) {
if (e instanceOf MyError) {
handleError()
}
else {
throw e;
}
}
The problem with the JavaScript version is that the re-thrown "e" will
have a different stack trace than when "e" is caught.
How can I preserve the stack trace so that the re-thrown "e" seems as
though it was never caught at all?
In the Java world, Hannes Wallnoefer suggested the following works
"The important thing is really to preserve the stack info of the
original exception, i.e. pass the original exception to the
RuntimeException constructor."
Various things I've tried in JavaScript running on Rhino has not been
able to preserve the stack trace. It is fine if a solution only works
on Rhino. Any suggestions?
Thanks,
Peter
http://groups.google.com/group/mozilla.dev.tech.js-engine.rhino/msg/f38dde56deac2237
When I execute the following I see "nothing" no matter which "throw"
line is used.
try {
throw new java.lang.Exception('hi');
// throw new Error('hi');
// throw "asdf";
}
catch (e) {
java.lang.System.out.println("vvvvvvvvvvvvvvvvvvvvvvvvv");
if (e.rhinoException != null) {
e.rhinoException.printStackTrace();
}
else if (e.javaException != null) {
e.javaException.printStackTrace();
}
else {
java.lang.System.out.println("nothing");
}
java.lang.System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^");
}
Is there a special version of Rhino that is needed for this or do I
need to set some flag or something else to get the extensions?
Thanks,
Peter
try{
doSomething();
}
catch(e if e instanceof Error){
handleError(e);
}
Kris
Does this work for you?
js> function g() {
> try {
> Packages.Test.f();
> } catch (e if e instanceof MyError) {
> print("caught");
> }
> }
js> function MyError() { }
js> g()
org.mozilla.javascript.WrappedException: Wrapped Test$MyException
(<stdin>#19)
at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:
1773)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:183)
at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:
247)
at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:
3331)
at script.g(<stdin>:19)
at script(<stdin>:19)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:
2487)
at org.mozilla.javascript.InterpretedFunction.call
(InterpretedFunction.java:164)
at org.mozilla.javascript.ContextFactory.doTopCall
(ContextFactory.java:398)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:
3056)
at org.mozilla.javascript.InterpretedFunction.exec
(InterpretedFunction.java:175)
at org.mozilla.javascript.tools.shell.Main.evaluateScript(Main.java:
564)
at org.mozilla.javascript.tools.shell.Main.processSource(Main.java:
424)
at org.mozilla.javascript.tools.shell.Main.processFiles(Main.java:
196)
at org.mozilla.javascript.tools.shell.Main$IProxy.run(Main.java:117)
at org.mozilla.javascript.Context.call(Context.java:515)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:
507)
at org.mozilla.javascript.tools.shell.Main.exec(Main.java:179)
at org.mozilla.javascript.tools.shell.Main.main(Main.java:157)
Caused by: Test$MyException
at Test.f(Test.java:4)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:161)
... 15 more
(In case it's not obvious, I replied before I saw Kris's post.)
--N
Kris and Norris, thanks.
What is that extension called? Is it strictly Rhino only?
One advantage of what I was thinking below was that the reraise
function could be extracted to a separate file and do something
rhino-specific. In a non-rhino environment, reraise could simply throw
the exception and not preserve the stack trace (unless there was some
way to preserve the stack trace in another environment.)
try {
doSomething();
}
catch (e) {
if (e instanceOf MyError) {
handleError(e);
}
else {
reraise(e);
}
}
Thanks,
Peter
I think it is called conditional catch and it's in Firefox, maybe
since
JavaScript ~1.5, I think.
Kris