throw object ignores toString() in error message

David P. Caldwell david at code.davidpcaldwell.com
Tue Sep 2 13:25:21 UTC 2014


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