Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)

What is Try Catch in JavaScript and how it works?

Last updated on Mar 13,2023 2.7K Views

10 / 31 Blog from JavaScript

No matter how good you are at programming, there will be errors in certain scripts. These errors might occur because of unexpected user input, an erroneous server response or any other reason. Try catch in JavaScript allows you to catch errors and, instead of dying, do something more reasonable. In this article, we will see how JavaScript uses the try-catch to handle the exception in the following sequence:

Let’s begin.

Handling Runtime Error in JavaScript

Error handling has been evolving since the ages of Netscape and IE4. You don’t need to settle for what the browser throws in your face in an event of a JavaScript error but instead, you can take the matter into your own hands. The try-catch statement of JavaScript helps you in rerouting when a JavaScript exception is encountered.

Along with other defensive coding techniques such as Object detection and the onError event, try-catch adds the ability to navigate around certain errors that in the past would have instantly stopped your script at its tracks.

What is Try Catch in JavaScript?

The latest versions of JavaScript added exception handling capabilities. JavaScript implements the try-catch 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. For more check out this Full Stack Developer Certification today.

The try statement is used to define a block of code to be tested for errors while it is being executed. Whereas, the catch statement is used to define a block of code to be executed if an error occurs in the try block.

The JavaScript statements try and catch come in pairs:

try {
Block of code to try
catch(err) {
Block of code to handle errors

When an exception occurs in the try block, the exception is placed in err and the catch block is executed.


Try Catch Example

Here is an example of JavaScript Try Catch:

var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "is empty";
if(isNaN(x)) throw "is not a number";
x = Number(x);
if(x > 10) throw "is too high";
if(x < 5) throw "is too low";
catch(err) {
message.innerHTML = "Input " + err;
finally {
document.getElementById("demo").value = "";


Output - try catch in javascript - edureka


With this, we have come to the end of our article. I hope you understood how try catch in JavaScript is used to catch errors.

Check out our Full Stack Web Developer Masters Program which comes with instructor-led live training and real-life project experience. This training makes you proficient in skills to work with back-end and front-end web technologies. It includes training on Web Development, jQuery, Angular, NodeJS, ExpressJS, and MongoDB.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 3rd August,2024

3rd August

SAT&SUN (Weekend Batch)
View Details
Java Course Online

Class Starts on 28th September,2024

28th September

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Try Catch in JavaScript and how it works?