61

Replace Exception with Precheck


Goal

Exceptions used for predictable, checkable conditions become an explicit precheck the caller can perform, leaving exceptions for truly exceptional cases.

Before the refactoring
try {
  return amounts[i] / 100;
} catch (e) {
  return 0;
}
After the refactoring
if (i >= amounts.length) return 0;
return amounts[i] / 100;
Savings

The error path is local and visible; reading code top-to-bottom describes the rules rather than the failure response; debuggers stop catching benign throws.

Note

Race conditions: the precheck may pass and the operation still fail (TOCTOU). Use prechecks only for conditions the caller can verify without a race.