try-catch in JavaScript
dheeraj koranga
1 min read
The try-catch
statement in JavaScript allows you to handle errors gracefully. It consists of two main blocks:
try
block: Contains code that may throw an error.catch
block: Executes if an error is thrown in thetry
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