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
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to