Skip to main content

Command Palette

Search for a command to run...

try-catch in JavaScript

Published
•1 min read•View as Markdown

The try-catch statement in JavaScript allows you to handle errors gracefully. It consists of two main blocks:

  1. try block: Contains code that may throw an error.

  2. catch block: Executes if an error is thrown in the try block. It catches the error and handles it to prevent the script from crashing.

Syntax:

  • error occurred when not handled using try-catch

  • error handling using try-catch

  • you can also print the error using catch(e) or catch(error)

More from this blog

dheeraj koranga's blog

51 posts