Replace Exception with Test

You are throwing an exception on a condition the caller could have checked first.

Change the caller to make the test first.

	double getValueForPeriod (int periodNumber) {
		try {
			return _values[periodNumber];
		} catch (ArrayIndexOutOfBoundsException e) {
			return 0;
		}
	}

	double getValueForPeriod (int periodNumber) {
		if (periodNumber >= _values.length) return 0;
		return _values[periodNumber];
	}

For more information see page 315 of Refactoring

| Refactoring Home | | Alphabetical List |