13. Exception handling #

Created Friday 11 March 2022

Why #

To handle exceptions and error, and provide a streamlined flow which segregates code functionality from the exceptional handling code.

How #

JavaScript has a 3 block structure for exception handling. There are 3 possible syntax constructs:

  1. trycatch
  2. tryfinally
  3. trycatchfinally

The role of blocks #

Return value priority #

If something is returned from the block (s), the whole value evaluates is equal to return value from:

  1. finally - highest priority, even if the catch itself has more exceptions.
  2. catch - If an exception was raised and there’s no finally (or it does not return anything).
  3. try - if there are no exceptions, or if catch/finally return nothing, or are not defined.

Nesting of try blocks #

try blocks may be nested inside each other.

This is not used much, except in code that uses the async-await syntax.

Source: MDN