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

Catch and re-throw in javascript without loosing stack info

636 views
Skip to first unread message

wolffiex

unread,
Nov 8, 2009, 2:18:58 PM11/8/09
to
Hi list,
I've got my application running great with Rhino. I'm at the point now
where I want to clean up my exception handling.

If I let my script exceptions percolate up to Java, I get correct
stack information about where the exception occurred. However, in
cases where I want to catch the exception in script, conditionally
process it, and maybe re-throw, I don't see how to get the original
exception back. I'm hoping there's an API call that I just missed.

Here's an example

doesThrow = function(){
//stupid, but illustrates the point
throw Math.random();
}

doesRethrow = function(){
try{
doesThrow()
}catch(e){
//half of the errors are caught, half want to be re-thrown
if (e > .5) {
//this is the place where I want to be able to really re-
throw the original
//exception, complete with the stack information that
includes the site
//where the original exception occurred
throw e;
} else {
....//this is ok
}
}
}

I'm still using 1.6, but would consider upgrading to the latest
release if it'd help with this.

Thanks in advance,
A

Daniel Friesen

unread,
Nov 8, 2009, 11:00:46 PM11/8/09
to
Side note, not completely solving re-throw issues, but JS 1.7 has a
useful feature that could help in most of your cases:

doesRethrow = function(){
try {
doesThrow();
} catch( e if e > .5 ) {
// ...
}
};

--
~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]

wolffiex

unread,
Nov 10, 2009, 1:50:52 AM11/10/09
to

That is helpful,thanks, though I'm still interested if there's answer
to the original question. After I posted this, I realized that it
would also solve my problem if I could manually create some kind of
exception that captured all the stack information.

0 new messages