Error Handling
try / catch / finally
try— code that might failcatch(err)— handle error;err.message,err.namefinally— always runs
Throw your own: throw new Error("msg")
try — code that might failcatch(err) — handle error; err.message, err.namefinally — always runsThrow your own: throw new Error("msg")
function divide(a,b){
if(b===0) throw new Error('Division by zero');
return a/b;
}
try{
console.log(divide(10,0));
}catch(e){
console.error(e.message);
}finally{
console.log('Done');
}
More in JavaScript