FAQ: Learn JavaScript: Error Handling - The try...catch Statement

This community-built FAQ covers the “The try…catch Statement” exercise from the lesson “Learn JavaScript: Error Handling”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise The try…catch Statement

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I think this was a terrible lesson for try and catch especially the final console.log and the explanation of it.

console.log('The thrown error that was caught in the try...catch statement!');
// Prints: 'The thrown error that was caught in the try...catch statement!'

Is pointless to have in the exercise

2 Likes

Hi, in this lesson, we are told to use catch(e) to catch the Error message. Why exactly e. Is this a Javascript convention ? e for Error ?

1 Like

Yes. Though you can name it whatever you want - error, hamburger, it really doesn’t matter

In the lesson, it states that letter e represents the thrown error. My question is, how does e in the catch block reference to the Error function block? Shouldn’t the Error function also have an e passed into the parenthesis as a parameter instead of the error message?

try {
 throw Error ('Incorrect, please try again.');
} catch(e) {
  console.log(e);
}

Also, if I wanted a function to throw multiple Errors within a single code block, I have no idea how the catch block would reference to a specific Error thrown in the Error block. Can I declare a local variable for each error? Is this possible? If so, is it considered best practice?

Hello stoutfella !

  1. Every error you have in your try will be passed as an argument of the catch statement. We used “e”’ as a parameter of the catch because it’s a shorthand for error… but you can also use ‘err’ or even ‘error’, it doesn’t matter. When a error occured on your try statement, it’ll become the parameter of your catch statement.

  2. You can throw multiple error in your try statement, and when the first error will happen, the next code in the try will stop and this error will be handled by the catch statement. It’s good because you can personalize differents message for the differents error then know what happened precisely and what give an error.

2 Likes