Replace Error Code with Exception

Removes smells
Compare
Symptom
Human

Functions returning numeric or string codes for failure — `withdraw(amount)` returning -1 for insufficient funds — relying on callers to check and propagate.

Agent

The agent finds functions returning numeric or string codes for failure; verifying error handling requires the agent to trace every caller and check whether the code is inspected.

Goal
Human

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

Agent

Failures throw exceptions the agent reasons about as separate control flow; the type system marks the failure path.

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;
}
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Forgetting to check silently swallows the error; the type system can't enforce the check; cleanup logic scatters across consumers.

Agent

Every caller is a chance to silently swallow the error; the agent verifying correctness must audit every call site for the check.

Tradeoff
Human

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

Agent

Exceptions for predictable conditions misuse the mechanism; the agent ships try/catch around expected outcomes that should be values.

Relief
Human

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

Agent

The agent reasons about success and failure paths separately; cleanup happens via finally / try-with; forgetting to handle no longer silently swallows.

Trap
Human

Using exceptions for predictable conditions — turning every error code into an exception introduces control flow via throws that callers must catch.

Agent

Throwing for predictable conditions (not-found, validation failure) makes expected outcomes look like bugs to the agent reading the catch blocks.