
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Explain try and catch statements in JavaScript with examples.
The try statement allows us execute a block of code and test for errors. Those errors are then caught and handle by the catch statement.
Following is the code for try and catch statement in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>Try and catch in JavaScript</h1> <div style="color: green;" class="result"></div> <button class="Btn">CLICK HERE</button> <h3> Click on the above button to call a variable before it is defined </h3> <script> let resEle = document.querySelector(".result"); document.querySelector(".Btn").addEventListener("click", () => { try { resEle.innerHTML = a; } catch (err) { resEle.innerHTML = "Error = " + err; } let a = 44; }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- Explain Try/Catch/Finally block in PowerShell
- Explain try, except and finally statements in Python.
- Can we write any statements between try, catch and finally blocks in Java?
- Try, catch, throw and throws in Java
- Try-Catch-Finally in C#
- Can we declare a try catch block within another try catch block in Java?
- How do we use try...catch...finally statement in JavaScript?
- Explain Optional Catch Binding in JavaScript.
- Explain about EventHandler with Examples in JavaScript
- Try/catch/finally/throw keywords in C#
- Explain the finally Statement in JavaScript with examples.
- Flow control in try catch finally in C#
- Can we define a try block with multiple catch blocks in Java?
- What is JavaScript closure? Explain with examples.
- Explain JavaScript Regular Expression modifiers with examples

Advertisements