60

Replace Error Code with Exception


Goal

Numeric or string error codes that callers must remember to check are replaced with exceptions that propagate by default.

Before the refactoring
function withdraw(amount) {
  if (amount > balance) return -1;
  balance -= amount;
  return 0;
}
After the refactoring
function withdraw(amount) {
  if (amount > balance) throw new InsufficientFunds();
  balance -= amount;
}
Savings

Forgetting to check no longer silently swallows the error; the type system marks the failure path; cleanup happens via finally / try-with.

Note

Exceptions for predictable conditions misuse the mechanism — only convert codes that represent genuine, exceptional, unrecoverable failures.