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;
		}
	}

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

For more information see page 310 of Refactoring

| Refactoring Home | | Alphabetical List |