Defining and Throwing Exceptions

Defining OutOfBoundsException

The file OutOfBoundsException.java:

/**
 * Thrown when the Location of a Wanderer object in a WanderBallField 
 * is outside the boundaries of the WanderBallField.
 * */

public class OutOfBoundsException extends RuntimeException {

    public OutOfBoundsException(int x, int y) {
		System.out.println("Invalid coordinates " + x + ", " + y);
    }
} // end OutOfBoundsException class

Throwing an OutOfBoundsException

From the drawWanderBaller method in WanderBallField.java
if ((posX < 0 || posX >= FIELD_WIDTH) ||
    (posY < 0 || posY >= FIELD_HEIGHT)) {
	// ... graphics code omitted ...
	// draw a dead wander baller and throw exception                                         
	// ... more graphics code omitted ...
	throw new OutOfBoundsException(posX, posY);
}