Error Handling in javascript
Error handling uses try/catch/finally blocks and the Error object. Throw custom errors when something goes wrong and catch them at the appropriate level. In async code, combine try/catch with async/await.
Example
function divide(a, b) {
if (b === 0) {
throw new Error("Cannot divide by zero");
}
return a / b;
}
try {
divide(4, 0);
} catch (err) {
console.error("Error:", err.message);
}Business rules (like 'no divide by zero') are enforced by throwing an Error; callers wrap the call in try/catch to handle failures gracefully.