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

Front End Web Development

Topics Covered
  • AngularJS (35 Blogs)
  • jQuery UI Development (1 Blogs)
  • The Complete WebDeveloper (43 Blogs)
  • ReactJS (7 Blogs)
SEE MORE

What is Try Catch in JavaScript and how it works?

Last updated on Apr 14,2022 1.8K 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 development course 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 = "";
}
}
</script>
 
</body>
</html>

Output:

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 Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 29th October,2022

29th October

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 26th November,2022

26th November

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
Send OTP
REGISTER NOW
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?

edureka.co