Replace Error Code with Exception

A method returns a special code to indicate an error.

Throw an exception instead.

int withdraw(int amount) {
  if (amount > _balance)
    return -1;
  else {
    _balance -= amount;
    return 0;
  }
}

image/svg+xml

void withdraw(int amount) throws BalanceException {
  if (amount > _balance) throw new BalanceException();
  _balance -= amount;
}