try-catch in JavaScript

dheeraj korangadheeraj koranga
1 min read

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)
0
Subscribe to my newsletter

Read articles from dheeraj koranga directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

dheeraj koranga
dheeraj koranga