Error Handling Patterns
Nested Handling
Handle one error locally and pass another outward.
Nested Handling
nested_handling.js
const mode = "inner";
let status = "";
try {
try {
if (mode === "inner") {
throw new Error("inner");
}
if (mode === "outer") {
throw new Error("outer");
}
status = "ok";
} catch (error) {
if (error.message === "outer") {
throw error;
}
status = "inner handled";
}
} catch (error) {
status = "outer handled";
}
console.log("mode=" + mode);
console.log("status=" + status);
nested-handling
Nested `try` blocks can handle some failures close to where they happen and allow other failures to move outward.