Lesson content
Read, practise, then check your understanding
throw accepts any value, but Error objects provide names, messages, stacks, and causes. Throw specific errors at clear contract boundaries.
Preserve context
function parseConfig(text) {
try {
return JSON.parse(text);
} catch (error) {
throw new SyntaxError("Invalid configuration", { cause: error });
} finally {
// unconditional follow-up, not usually return
}
}
Catch only when you can recover, add context, or report at a boundary. Avoid swallowing errors or returning from finally. Custom Error subclasses can carry stable codes. Promise rejections require awaited try/catch or chain rejection handlers; global unhandled hooks are last-resort reporting, not normal recovery.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.