
- 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
How to catch exceptions in JavaScript?
To catch exceptions in JavaScript, use try…catch…finally. JavaScript implements the try...catch...finally construct as well as the throw operator to handle exceptions.
You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax errors.
Example
You can try to run the following code to learn how to catch exceptions in JavaScript −
<html> <head> <script> <!-- function myFunc() { var a = 100; try { alert("Value of variable a is : " + a ); } catch ( e ) { alert("Error: " + e.description ); } finally { alert("Finally block will always execute!" ); } } //--> </script> </head> <body> <p>Click the following to see the result:</p> <form> <input type = "button" value = "Click Me" onclick = "myFunc();" /> </form> </body> </html>
- Related Articles
- How to catch all JavaScript unhandled exceptions?
- How to catch all the exceptions in C++?
- Is it possible to catch multiple Java exceptions in single catch block?
- How to catch many exceptions at the same time in Kotlin?
- How to catch multiple exceptions in one line (except block) in Python?
- How to use Custom Exceptions in JavaScript?
- How to define custom JavaScript exceptions?
- How to catch syntax errors in JavaScript?
- How to catch all JavaScript errors?
- How to catch any JavaScript exception in Clojurescript?
- How does Exceptions Handling work in JavaScript?
- How to Handle Exceptions in Ruby
- How to use finally on promise with then and catch in Javascript?
- Explain Optional Catch Binding in JavaScript.
- How do we use try...catch...finally statement in JavaScript?

Advertisements