public abstract class List { public abstract Object getFirst(); } public class Empty extends List { public Object getFirst() { throw new RuntimeException( "Empty has no first"); } } public class Larger extends List { private Object first; private List rest; public Larger( Object first, List rest ) { this.first = first; this.rest = rest; } public Object getFirst() { return this.first; } } class Window { int width, height; boolean displayed = false; Window( int width, int height ) { if ( width < 0 ) throw new RuntimeException("Window requires a positive width"); if (height < 0) throw new RuntimeException("Window requires a positive height"); this.width = width; this.height = height; } void display() { displayed = true; } void drawPixels(Object pixels) throws WindowHiddenException { if ( displayed ) pixels.equals(pixels); //Do something to draw the pixels else throw new WindowHiddenException("Window must be visible to draw"); } } class Graphics { Window myView; Graphics( Window myView ) { this.myView = myView; } void alwaysDraw() { try { myView.drawPixels( "Draw a square"); } catch ( WindowHiddenException e ) { myView.display(); this.alwaysDraw(); } } } class WindowHiddenException extends Exception { WindowHiddenException( String s ) { super( s ); } }