Web Analytics

Error Handling

Practical ~15 min read

The try...catch Statement

The try statement defines a code block to run (to try).

The catch statement defines a code block to handle any error.

The finally statement defines a code block to run regardless of the result.

HTML
CSS
JS

The throw Statement

The throw statement allows you to create a custom error.

HTML
CSS
JS

The finally Statement

The finally statement lets you execute code, after try and catch, regardless of the result.

HTML
CSS
JS

Summary

  • Use try...catch to handle runtime errors gracefully.
  • Use throw to create custom errors.
  • Use finally to execute code regardless of the outcome (e.g., cleanup).

Quick Quiz

Which block is executed regardless of the result?

A
try
B
catch
C
finally
D
throw