Chapter 10: Error handling
10.1 Error handling, "try..catch"
The “try…catch” syntax
try..catch
only works for runtime errors, not syntax errors.
try..catch
works synchronously, which means that it cannot catch the errors in the scheduled function such as setTimeout
, unless the try..catch
is inside that function.
Error object
name
: Example:ReferenceError
message
: Textual message about error details.stack
: A string with information about the sequence of nested calls that led to the error.
Using “try…catch”
If we don’t need error details, catch
may omit it.
Throwing our own errors
JavaScript has many built-in constructors for standard errors: Error
, SyntaxError
, ReferenceError
, TypeError
and others.
Rethrowing
Catch should only process errors that it knows and “rethrow” all others.
try…catch…finally
If finally
exists, it runs in all cases:
After
try
, if there were no errors.After
catch
, if there were errors.
The finally
clause is often used when we start doing something and want to finalize it in any case of outcome.
Variables are local inside 1try..catch..finally`.
Before the try
returns the code, finally
is executed just before it.
The try..finally
construct, without catch
clause, is also useful.
Global catch
message
: Error message.url
: URL of the script where error happened.line
,col
: Line and column numbers where error happened.error
: Error object.
10.2 Custom errors, extending Error
Extending Error
Further inheritance
Last updated