try / catch

throw and catch a TypeError.

throw raises. catch receives the error. Log err.message.

Goal

Catch a thrown Error and a JSON parse failure.

try {
  throw new Error("empty till");
} catch (err) {
  console.log(err.message);
}
try {
  JSON.parse("{");
} catch (err) {
  console.log(err.name);
}
function needCity(city) {
  if (!city) throw new Error("city required");
  return city;
}
try {
  console.log(needCity("Nairobi"));
  console.log(needCity(""));
} catch (err) {
  console.log("caught", err.message);
}
console.log(typeof new TypeError("bad"));