Skip to content

Instantly share code, notes, and snippets.

@whal-e3
Created November 23, 2020 02:52
Show Gist options
  • Save whal-e3/1aa90c24044010cba764d400bd58f97a to your computer and use it in GitHub Desktop.
Save whal-e3/1aa90c24044010cba764d400bd58f97a to your computer and use it in GitHub Desktop.
JS error handling (try/catch/finally/throw)
const user = { email: 'jdoe@gmail.com' };
try {
if (!user.name) {
// throw 'User has no name';
throw new SyntaxError('User has no name');
}
} catch (e) {
console.log(`User Error: ${e.message}`);
// console.log(e.message);
// console.log(e.name);
// console.log(e instanceof ReferenceError);
} finally {
console.log('finally');
}
console.log('program continued');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment