The throw statement lets you create your own errors. When you detect a problem, throw an error to signal it to callers.
function divide(a, b) {
if (b === 0) {
throw new Error("Cannot divide by zero");
}
return a / b;
}
Throw stops execution and transfers control to the nearest catch block.