throw object ignores toString() in error message

A. Sundararajan sundararajan.athijegannathan at oracle.com
Wed Sep 3 14:20:03 UTC 2014


Part of the reason to avoid calling out toString is that toString itself 
could throw exception thereby masking original exception (for 
debugging). Perhaps we could call toString within try..catch and show 
that only if it succeeds.. I'll file a bug.

-Sundar

On Tuesday 02 September 2014 06:55 PM, David P. Caldwell wrote:
> Not a "bug," I suppose, as the implementation can probably do whatever
> it wants under the standard. But I think it'd be better to use
> toString().
>
> Here's an illustration (for clarity, the "throw-number.js" case is an
> illustration of the behavior I would call "correct" or "optimal"):
>
> $ echo "throw-object.js"; cat throw-object.js; jrunscript
> throw-object.js; echo "throw-number.js"; cat throw-number.js;
> jrunscript throw-number.js
> throw-object.js
> (function() { throw { toString: function() { return "it"; } } })()
> script error in file throw-object.js : [object Object] in
> throw-object.js at line number 1 at column number 14
> throw-number.js
> (function() { throw 3; })()
> script error in file throw-number.js : 3 in throw-number.js at line
> number 1 at column number 14
>
> For what it's worth, the Chrome Developer Tools do something more like
> what one would expect, showing the toString() of the thrown object in
> red and allowing expansion of its properties. Firefox behaves
> similarly, except it does not allow expansion of the thrown object's
> properties. Safari behaves like Chrome, except it also gives the
> thrown object a "stack" property in the interim.
>
> Rhino also does something more like what one would expect:
>
> $ echo "throw-object.js"; cat throw-object.js; java -jar
> ~/Downloads/rhino-1.7R3.jar throw-object.js; echo "throw-number.js";
> cat throw-number.js; java -jar ~/Downloads/rhino-1.7R3.jar
> throw-number.js
> throw-object.js
> (function() { throw { toString: function() { return "it"; } } })()
> js: "throw-object.js", line 1: exception from uncaught JavaScript throw: it
> throw-number.js
> (function() { throw 3; })()
> js: "throw-number.js", line 1: exception from uncaught JavaScript throw: 3
>
> -- David P. Caldwell
> http://www.davidpcaldwell.com/



More information about the nashorn-dev mailing list