Jump to Categories

try...catch

try {
  // Not defined throws an error, goes to catch
  callAFunc();
}
catch(e) {
  console.log(e)
}

Error

try {
  lalala; // error, variable is not defined!
} catch(err) {
  alert(err.name); // ReferenceError
  alert(err.message); // lalala is not defined
  alert(err.stack); // ReferenceError: lalala is not defined at ...

  // Can also show an error as a whole
  // The error is converted to string as "name: message"
  alert(err); // ReferenceError: lalala is not defined
}

References