Why is crashing better than staying quiet?
Why an error with a loud message beats a program that "sort of works".
function priceWithTax(price) {
try {
return price.amount * 1.2;
} catch {
return 0;
}
}
console.log(priceWithTax({ amount: 100 }));
console.log(priceWithTax(undefined));The error didn't disappear — it turned into wrong data. A $0 item lands in the cart, in the order, in the accountant's report. A week later someone notices strange revenue, and the cause has to be hunted through the whole system.
Had the function crashed straight away, the message would have pointed to the exact spot: the file, the line, which value was undefined. That's the fail fast principle — "break immediately and loudly".
The same question comes up when you write a function. The cat wasn't found — what do you return? The easy option is null. But null doesn't say what happened and doesn't stop the program. It quietly travels on until someone asks it for .age — and the crash happens there, a hundred lines away from the real cause.
const cats = [{ name: "Mittens", age: 2 }];
function findCatQuiet(name) {
for (const cat of cats) if (cat.name === name) return cat;
return null;
}
function findCatLoud(name) {
for (const cat of cats) if (cat.name === name) return cat;
throw new Error("Cat \"" + name + "\" isn't in the database");
}
try {
const cat = findCatQuiet("Felix");
// …a hundred lines later
console.log(cat.age);
} catch (error) {
console.log("Quiet:", error.message);
}
try {
findCatLoud("Felix");
} catch (error) {
console.log("Loud:", error.message);
}null. The second names the cause right away.null is honest when "nothing found" is a normal answer, not a breakage: a catalogue search, an empty cart. But if you can't carry on without a result, throw an error with a clear message. Whoever knows what to do will catch it; whoever doesn't will let it through.
Catch errors where you know what to do about them:
- broken JSON from localStorage → use the default settings;
- the network is down → show "No connection, try again";
- the user typed an invalid age → highlight the field.
An unexpected error is better let through upwards: there a monitoring system records it, and a developer sees it.
How do you catch only "your" error? Every error has a name. Check it in catch, and throw anyone else's error further: throw error. That's called rethrowing. Watch what a broad catch does to an ordinary typo.
function parseAge(text) {
const age = Number(text);
if (Number.isNaN(age)) throw new RangeError("Age is not a number: " + text);
return age;
}
function readAgeBroad(text) {
try {
return parseAg(text); // a typo in the name!
} catch {
return 0;
}
}
function readAgeNarrow(text) {
try {
return parseAg(text); // the same typo
} catch (error) {
if (error.name !== "RangeError") throw error; // not ours: let it fly
return 0;
}
}
console.log(readAgeBroad("3"));
try {
readAgeNarrow("3");
} catch (error) {
console.log(error.name + ": " + error.message);
}catch turned a typo into "age 0". The narrow one let the ReferenceError through — and the typo shows at once.Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.