Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics Handling Errors in Node Handle Status Code Errors

Why so many error-checkings? Isn't there one try-catch block able to capture them all?

If the whole code is inside a try-catch block, shouldn't it care for catching all errors that occur inside it?

1 Answer

Rohald van Merode
Rohald van Merode
Treehouse Staff

Hi Murilo de Melo 👋

You could indeed wrap everything in a single try/catch block, but when you use a try/catch block for each asynchronous action you'll have more control over your error handling, You'll know where the error originated from and therefor you'll be able to give more specific error messaging to the user 🙂 In the example shown in the video the initial try/catch catches errors happening when something went wrong while fetching the data, and the nested catch will only run specifically when something went wrong when parsing that data.

In this specific video however both are doing the exact same thing: printError(error). So for this specific example you could in theory get away with a single try/catch 🙂

Hope this clears things up!

Hi Rohald

It did cleared things up.