Catch & Handle Runtime Errors in JavaScript

JavaScript·2 min read·Jan 1, 2026

In programming, exception handling is the process of dealing with unwanted or unexpected events (essentially errors) that might disrupt the normal execution flow of a computer program.

In Node.js, some of these errors like runtime errors can be caught or ignored, which means that the process can decide to either intercept them and perform an action in response, or just completely ignore them.

On the other hand, other errors like syntax errors cannot be caught or ignored, which means that the process will be forced to immediately terminate and the error must be corrected manually in order for the program to run as intended.

The try...catch statement

To prevent your application from unexpectedly crashing due to a runtime error, you can execute code that may throw an error within a try block.

If an error is thrown at runtime, the execution flow of the code will be altered and the control transferred automatically to the catch block. Once the statements in the catch block have been executed, the script will resume its normal, sequential execution.

try {  // statements} catch(error) {  // handle or ignore}

Where:

  • error is the value of the error thrown within the try block.

Note: The try block must always be used with a catch block, as otherwise the errors won't be caught and the process crash.

Unlock the program 🚀

Pay once, own it forever.

€79

30-day money-back guarantee

  • 13 modules
  • 113 lessons with full-code examples
  • 29 projects with commented solutions
  • All future lesson and project updates
  • Lifetime access

By submitting this form, you agree to the Terms & Conditions and Privacy Policy.