Skip to content

Latest commit

 

History

History
45 lines (34 loc) · 1023 Bytes

Error-Handling.md

File metadata and controls

45 lines (34 loc) · 1023 Bytes

Error Handling

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.

throw

throw 'someException'; 

try...catch

function fetchData() {
  throw 'fetchingError'; 
}

function logError(error) {
  console.log(error); 
}

try {
  fetchData(); 
} catch(error) {
  console.log('Error occured');
  logError(error); 
}

try...catch...finally

try {
  throw 'someErrorThrownHere'; 
} catch(error) {
  logError(error); 
} finally {
  console.log('This block of code will always execute'); 
}

Resources

  1. MDN - throw
  2. MDN - Error