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

re-throwing exceptions

5 views
Skip to first unread message

Peter Michaux

unread,
Nov 30, 2008, 3:41:03 AM11/30/08
to dev-tech-js-...@lists.mozilla.org
I have a piece of code using a try-catch where I really only want to
catch exceptions under certain conditions. For example. In Java I
could write the following to catch only a certain type of exception

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

Peter Michaux

unread,
Nov 30, 2008, 2:16:25 PM11/30/08
to dev-tech-js-...@lists.mozilla.org
I found this idea about Rhino extensions to error objects.

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

Kris Zyp

unread,
Dec 1, 2008, 9:30:43 AM12/1/08
to
Is this what you want?:

try{
doSomething();
}
catch(e if e instanceof Error){
handleError(e);
}

Kris

Norris Boyd

unread,
Dec 1, 2008, 9:41:52 AM12/1/08
to
On Nov 30, 2:16 pm, "Peter Michaux" <petermich...@gmail.com> wrote:
> I found this idea about Rhino extensions to error objects.
>
> http://groups.google.com/group/mozilla.dev.tech.js-engine.rhino/msg/f...

>
> 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

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


Norris Boyd

unread,
Dec 1, 2008, 10:36:07 AM12/1/08
to

(In case it's not obvious, I replied before I saw Kris's post.)

--N

Peter Michaux

unread,
Dec 1, 2008, 1:01:08 PM12/1/08
to Kris Zyp, dev-tech-js-...@lists.mozilla.org
On Mon, Dec 1, 2008 at 6:30 AM, Kris Zyp <kri...@gmail.com> wrote:
> Is this what you want?:
>
> try{
> doSomething();
> }
> catch(e if e instanceof Error){
> handleError(e);
> }

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

Kris Zyp

unread,
Dec 1, 2008, 1:08:22 PM12/1/08
to
> What is that extension called? Is it strictly Rhino only?

I think it is called conditional catch and it's in Firefox, maybe
since
JavaScript ~1.5, I think.

Kris

Kris Zyp

unread,
Dec 1, 2008, 1:05:49 PM12/1/08
to Peter Michaux, dev-tech-js-...@lists.mozilla.org
0 new messages