Conditional catch-blocks: combining try...catch blocks with if...else structures?

So the documentation on this is pretty scarce; MDN web docs spares hardly twenty words and the two code block examples don’t make much sense to me.
What I’m supposed to do is test an object, catch an error, and return a specific error message based on the type of error.

If name is an empty string, return “Unknown”.
If name is not a string, throw a TypeError: “Expected name to be string”.
If there is no name, throw a TypeError: “Missing name”.
If there is no person, throw a TypeError: “Missing person”.

One variation of an erroneous object:

let invalidPerson = {
  person: {
    name: ""
  }
};

This is what I’ve come up with just for the first error, but it doesn’t work.

function displayName(obj) {
  try{ return obj.person.name;
     } catch (error) {
       if(obj.person.name = "") {
          return "Unknown"
       }  
     }
}

So what’s going on here? What am I missing?

hey @tcholmes3,

you put the code your testing in the try block then you can throw an error to the catch block , it would be structured like this…

function displayName(obj) {
 
  const name = obj.person.name
  try{ 
     if(name === "") {
      throw 'Unknown'
     } 
     if(typeof name !== 'string') {
       throw 'Expected name to be string'
     } 

     } catch (error) {
      return error
     }
}
2 Likes

@biscuitmanz Now that I know it seems so simple!
Round 2, I’ve run into another issue. I want the loop to continue through every object in the array, but catch obviously interrupts this process and executes everything in the catch-block. How do I make it so the getAllNames function returns an array and logs every error? This is as far as I’ve gotten:

function displayName(sketch) {
    try {
       if(sketch.artist === undefined) {
        throw new TypeError("Missing artist");
       } 
       if(sketch.artist.name === undefined) {
           throw new TypeError("Missing artist name")
       }
       if(typeof sketch.artist.name !== 'string') {
       throw new TypeError("Expected name to be a String")
       }
       if(sketch.artist.name === "") {
           return "Unknown";
       } else {
           return sketch.artist.name;
       }  
    } catch (error) {
        throw error;
    }
}

function getAllNames(arr) {
    let sketches = [];
    try {
        for(let sketch of arr){
        sketches.push(displayName(sketch));
    }
    } catch (error) { 
        console.log(error.message);
        return sketches;
    }
}

This exercise has been a real b***ard.

hey @tcholmes3 is this a challenge in the fcc curriculum? if it is could you post a link to the challenge?

I am in the wrong section. I should be in…Front End Help?

oh is this just your own project? im not quite sure what your tying to achieve here mate, you cant put type errors in a loop because throw stops the program because there is an error, if you let me know what your trying to do here i might be able to help more.

It’s the 2nd challenge here: https://www.qualified.io/assess/5e8cb4770b3d890013d6f00b

The prompt:

Get names from all sketches

Now that we’ve got a way to get the name (or an error) from a sketch, we need a function that will get the names from an array of sketches.
Write a function getAllNames that takes in an array of sketches. Some of them will be valid sketches, with artists with string names. Others will be invalid, in one of the ways we guarded against in displayName .

getAllNames should loop through the list of sketches, and for each one, use displayName to get the name and add it to a result array. Using try and catch , if there’s an error, catch it, and console.log the error message.

The function should return an array of all the names from all the valid sketches, and log all the errors.

1 Like

@tcholmes3,

ok i get what you mean now so you only want to pass the names to the array if they don’t get get an error, your code looks good but i think you just have a couple of things in the wrong places and you should only need the try catch on one of the functions but you have it on both.

try this…

function displayName(sketch) {
  if (sketch.artist === undefined) {
    throw new TypeError("Missing artist");
  }
  if (sketch.artist.name === undefined) {
    throw new TypeError("Missing artist name");
  }
  if (typeof sketch.artist.name !== "string") {
    throw new TypeError("Expected name to be a String");
  }
  if (sketch.artist.name === "") {
    return "Unknown";
  }
  return sketch.artist.name;
}

function getAllNames(arr) {
  let sketches = [];
  for (let sketch of arr) {
    try {
      sketches.push(displayName(sketch));
    } catch (error) {
      console.log(error.message);
    }
  }
return sketches;
}

1 Like

Gah! Of course! Wrapping try & catch in the for loop instead of placing the for loop in the try block continues the iteration. Haha, I just learned about try & catch the day before yesterday; clearly my difficulties with this exercise stem from not knowing what can be within either block. This has been very enlightening. Thank you, sir!

1 Like