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!

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 2017 Create a Command Line Weather Application Handling Errors - Solution

Handling Errors - Solution (Node.js)

I am getting very confused about error handling. I thought that try...catch statements bubble up and handle any error thrown within the try block. However the try...catch statement that surrounds the code in the get function below does NOT catch the error of having a mal-formed URL like its supposed to according to the instructor's video. When I change the url to something invalid, it warns me in the console that the error thrown is unhandled. I can ONLY get the proper error message handled if I do.... request.on('error', printError);

So my question is what then is the point of the try... catch block? What errors will it actually catch?

function printError(error) {

function get(query) {
      const parameters = {
        APPID: api.key,
        units: 'imperial'

      const zipCode = parseInt(query);
      if (!isNaN(zipCode)) {
        parameters.zip = zipCode + ',us';
      } else {
        parameters.q = query + ',us';

      const url = `https://api.openweathermap.org/data/2.5/weather?${querystring.stringify(parameters)}`;

      const request = https.get(url, response => {
        if (response.statusCode === 200) {
          let body = '';
          // Read the data
          response.on('data', chunk => {
            body += chunk;
          response.on('end', () => {
            try {
              //Parse data
              const weather = JSON.parse(body);
              //Print the data
            } catch (error) {
              //Parser error
        } else {
          // Status error code
          const statusErrorCode = new Error(`There was an error getting the message for "${query}". (${http.STATUS_CODES[response.statusCode]})`);
    } catch (error) {
James Crosslin
.a{fill-rule:evenodd;}techdegree seal-36
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,881 Points

I also do not get handling errors in general, and this objective gave me quite the headache. I've even worked on a few outside projects that just seem to not have errors, so I have no clue. Hopefully someone comes along and answers this.

1 Answer

Yes, I also struggled here. Even direct cut and paste showed that the incorrect URL gave out unhandled error messages. I'm not sure how much is due to the API being deprecated or any other issue.

In fairness, I had used an online course from Udemy on this topic and they had to update the course due to a deprecating weather API as well. It must make it difficult to keep up with.