Built-in error types — TypeError, RangeError, SyntaxError and when each appears¶
Level: 101 · for anyone who has read an error and wondered why it has that type
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: The engine picks the type: TypeError for using a value the wrong way, ReferenceError for an unknown name, RangeError for a number out of range, SyntaxError for text it cannot parse — and EvalError only when the host forbids eval altogether.
Keywords: TypeError, RangeError, SyntaxError, ReferenceError, URIError, EvalError
What the finished page will answer¶
- Which error type do
null.x,undefined(), assigning to aconstand1n + 1throw? - What throws
RangeError—new Array(-1),(1).toFixed(101), recursion without end — and what do the messages say? - Why is a
SyntaxErrorin your own file thrown before its first line runs, whileJSON.parse("{")throws at run time and can be caught? - When is
ReferenceErrorthrown for a name that is declared? - Which built-in error does the specification never throw, and what makes V8 throw it anyway?
- Which parts of an error can a program rely on — the constructor and
name— and which differ between engines, such as the message text?
Examples it will need¶
-
error_types_by_mistake_js.js— one line per mistake: the code, the error type it throws and the message -
error_types_syntax_before_run_sh.sh— a file whose first line logs and whose second is a syntax error: nothing printed, the SyntaxError and the exit status -
error_types_evalerror_sh.sh—evalunder--disallow-code-generation-from-stringsthrowingEvalError
See also¶
throwandtry...catch— catching the errors listed here- Custom errors and
cause— adding your own type to the list - Recursion and the call stack — the
RangeErrorthat ends runaway recursion - Hoisting and the temporal dead zone — the
ReferenceErrorfor a declared name used too early evalandnew Function— code run from a string, and the flag that forbids it