SyntaxStudy
Sign Up
JavaScript Beginner 1 min read

Error Handling

Error Handling

try / catch / finally

  • try — code that might fail
  • catch(err) — handle error; err.message, err.name
  • finally — always runs

Throw your own: throw new Error("msg")

Example
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');
}