Re-throwing errors

Brandon Benvie brandon at brandonbenvie.com
Fri May 11 13:13:42 PDT 2012


V8 provides a way to capture a stack trace on a specific function (that is
part of the current callstack/has live arguments and caller information the
debuffer can pull from). By using Error.captureStackTrace(errorObject,
functionToMakeStackFrom), you cause an accessor to be defined on the error
object. The getter function in the accessor holds the string version of the
stack and returns it regardless of context or whether it's called as an
accessor. If you rerun Error.captureStackTrace on an error that already has
a stack trace it will just overwrite it.

On Fri, May 11, 2012 at 2:43 PM, Brendan Eich <brendan at mozilla.com> wrote:

> Domenic Denicola wrote:
>
>> Consider:
>>
>> try {
>>    doStuff();
>> } catch (e) {
>>    console.log("uh oh, got an e", e);
>>    throw e;
>> }
>>
>> In Node and in all browsers I've tested with, this currently loses the
>> stack trace for `e`, and more importantly loses debugger step-back-ability
>> in tools like Firebug and Web Inspector.
>>
>> One solution would be to hope V8 and the browser vendors simply don't
>> throw away this stack trace, like they do currently. This seems like a win
>> to me, but the fact that it hasn't happened implies there must be something
>> wrong with the idea, e.g. maybe it breaks down in less-than-trivial cases.
>> Is that true?
>>
>
> So V8 overwrites e.stack on the re-throw?
>
> SpiderMonkey does not:
>
> js> function f() {
>
> try {
>    doStuff();
> } catch (e) {
>    console.log("uh oh, got an e", e);
>    throw e;
> }
> }
> js> function g() {f()}
> js> function h() {g()}
> js> function doStuff() {uh.oh}
> js> console = {log: print}
> ({log:function print() {[native code]}})
> js> try {h()}catch (e){E=e}
> uh oh, got an e ReferenceError: uh is not defined
> (new ReferenceError("uh is not defined", "typein", 11))
> js> e.stack
> typein:15: ReferenceError: e is not defined
> js> E.stack
> "doStuff at typein:11\nf at typein:**3\ng at typein:9\nh at typein:10\n@**typein:14\n"
>
> Since the explicit re-throw is not creating the exception object, it
> should not be mutating it to clobber the original .stack value.
>
> /be
>
>  If so, then in C++ and C# they solve this by saying that an empty `throw`
>> is an explicit re-propagation [1]. Would this be a possible feature to
>> introduce into Harmony?
>>
>> [1]: http://winterdom.com/2002/09/**rethrowingexceptionsinc<http://winterdom.com/2002/09/rethrowingexceptionsinc>
>> ______________________________**_________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss>
>>
> ______________________________**_________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120511/f0efa972/attachment-0001.html>


More information about the es-discuss mailing list