
- 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 does Exceptions Handling work in JavaScript?
JavaScript uses try…catch…finally to handle exceptions. The latest versions of JavaScript added exception-handling capabilities. 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.
Syntax
Here is the try...catch...finally block syntax −
<script> <!-- try { // Code to run [break;] } catch ( e ) { // Code to run if an exception occurs [break;] } [ finally { // Code that is always executed regardless of // an exception occurring }] //--> </script>
Example
You can try to run the following code to learn how exceptions are handled in JavaScript −
<html> <head> <script> <!-- function myFunc() { var x = 20; try { alert("Value of variable a is : " + a ); } catch ( e ) { alert("Error: " + e.description ); } } //--> </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 do exceptions work in C++
- How does internationalization work in JavaScript?
- How does JavaScript .prototype work?
- How does recursion function work in JavaScript?
- How does asynchronous code work in JavaScript?
- How does JavaScript Variable Scope work?
- How does the “this” keyword work in JavaScript?
- How does JavaScript work behind the scene?
- How does inline JavaScript work with HTML?
- How to catch exceptions in JavaScript?
- How does JavaScript 'return' statement work?
- How to use Custom Exceptions in JavaScript?
- How to define custom JavaScript exceptions?
- How does jQuery.scrollTop() work?
- How does jQuery.scrollLeft() work?

Advertisements