FAQ: Learn JavaScript: Error Handling - Error Handling Review

This community-built FAQ covers the “Error Handling Review” 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 Error Handling Review

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 am testing the following code:

try {
const Cat = {
name: ‘Marta’;
breed: ‘Siamese’
}
} catch (e) {
console.log(e);
}

console.log( ‘JS still works.’ );

The error (:wink: seems not to be caught, since I receive the message:

/home/ccuser/workspace/error-handling-error-handling-review/main.js:3
name: ‘Marta’;
^
SyntaxError: Unexpected token ;

(highlighted by red) and the string in the last statement is not printed to the console. What can be the reason?

There is the culprit, the semi-colon. Only commas are permitted in plain object syntax.

1 Like

Yes. This is a deliberate mistake which I made in order to check the try…catch construction. This construction worked when I “forgot” quotes around the cat’s name, but in the case of semicolon it doesn’t. I don’t understand why it is so.

you can’t catch syntax errors with try catch:

https://stackoverflow.com/questions/5963045/can-syntax-errors-be-caught-in-javascript

try catch is used for runtime errors (like TypeError)

4 Likes

I fell in the same trap. Nowhere in the explanation does it say that try…catch can only be used with runtime errors. Perhaps this can be added to the explanation or posted as a forum question at the end of the lesson in the Community Forums section?