Notes on how to handle exceptions using try-catch statements in JavaScript.
try {
console.log('hoge') ;
} catch (e) {
console.log('Could not process.') ;
}
// => hoge
try {
console.log(undifinedVariable) ;
} catch (e) {
console.log('Could not process.') ; console.log(e) ;
}
// => Could not process.
// => VM6073:5 ReferenceError: undifinedVariable is not defined
// => at <anonymous>:3:15
You can add a finally block to execute code regardless of whether an exception occurs.
To throw an error, use throw.
throw "Oh no! An error seems to have occurred!!";