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 'someException';
function fetchData() {
throw 'fetchingError';
}
function logError(error) {
console.log(error);
}
try {
fetchData();
} catch(error) {
console.log('Error occured');
logError(error);
}
try {
throw 'someErrorThrownHere';
} catch(error) {
logError(error);
} finally {
console.log('This block of code will always execute');
}