G
Garrett Smith
I recently came across a piece of code that defines SyntaxError. That
first jumped out at me as being really wrong. After about 5 seconds, I
got to thinking about the implications of doing that.
Basically, the code is something like:
| function SyntaxError(message, line, col) {
| this.col = col;
| this.line = line;
| this.message = message;
| }
The (overly commented) code is here:
http://github.com/nzakas/parser-lib/blob/new-css-lexer/src/util/SyntaxError.js
Now I got to wondering that when a SyntaxError occurs, should it throw
the redefined SyntaxError or should it throw a SyntaxError as defined in
the specification? As I understand the spec, it should throw the error
defined in the specification:
e error that must be thrown?
| 15.11.7 NativeError Object Structure
| When an ECMAScript implementation detects a runtime error, it throws
| an instance of one of the NativeError
| objects defined in 15.11.6. Each of these objects has the structure
| described below, differing only in the name
| used as the constructor name instead of NativeError, in the name
| property of the prototype object, and in the
| implementation-defined message property of the prototype object.
The same question can be asked for any of the NativeError types
(TypeError, ReferenceError, etc).
However, if there is an instanceof (or equivalent) check to inspect a
thrown error, that would fail:
function SyntaxError(){}
try {
eval("+++");
} catch(ex) {
alert(ex instanceof SyntaxError);
}
Should result "false".
Garrett
first jumped out at me as being really wrong. After about 5 seconds, I
got to thinking about the implications of doing that.
Basically, the code is something like:
| function SyntaxError(message, line, col) {
| this.col = col;
| this.line = line;
| this.message = message;
| }
The (overly commented) code is here:
http://github.com/nzakas/parser-lib/blob/new-css-lexer/src/util/SyntaxError.js
Now I got to wondering that when a SyntaxError occurs, should it throw
the redefined SyntaxError or should it throw a SyntaxError as defined in
the specification? As I understand the spec, it should throw the error
defined in the specification:
e error that must be thrown?
| 15.11.7 NativeError Object Structure
| When an ECMAScript implementation detects a runtime error, it throws
| an instance of one of the NativeError
| objects defined in 15.11.6. Each of these objects has the structure
| described below, differing only in the name
| used as the constructor name instead of NativeError, in the name
| property of the prototype object, and in the
| implementation-defined message property of the prototype object.
The same question can be asked for any of the NativeError types
(TypeError, ReferenceError, etc).
However, if there is an instanceof (or equivalent) check to inspect a
thrown error, that would fail:
function SyntaxError(){}
try {
eval("+++");
} catch(ex) {
alert(ex instanceof SyntaxError);
}
Should result "false".
Garrett