Java Q&A by Oliver Goldman Listing One // Strategy: Suppress // Initialization for a class that has nothing to do with I/O public void initialize() { try { // Load pre-computed values for this class. InputStream is = new FileInputStream( CACHE_FILE ); is.read( cache ); ... } catch( IOException ex ) { // Ignore: donít know what to do about this error, and // signature doesnít allow it to propagate. } } Listing Two // Strategy: Bail Out // Initialization for a class that has nothing to do with I/O public void initialize() { try { // Load pre-computed values for this class. InputStream is = new FileInputStream( CACHE_FILE ); is.read( cache ); ... } catch( IOException ex ) { // After all, isnít this why printStackTrace() exists? ex.printStackTrace(); System.exit( 1 ); } } Listing Three // Strategy: Propagate // Initialization for a class that has nothing to do with I/O public void initialize() throws IOException { // Load pre-computed values for this class. InputStream is = new FileInputStream( CACHE_FILE ); is.read( cache ); ... } Listing Four // Strategy: Base Case // Initialization for a class that has nothing to do with I/O public void initialize() throws Exception { // Load pre-computed values for this class. InputStream is = new FileInputStream( CACHE_FILE ); is.read( cache ); ... } Listing Five // Strategy: Wrap // Initialization for a class that has nothing to do with I/O public void initialize() throws MyAPIException { // Load pre-computed values for this class. try { InputStream is = new FileInputStream( CACHE_FILE ); is.read( cache ); } catch( IOException ex ) { throw new MyAPIException( "Loading cache failed", ex ); } ... } Listing Six // Unwrapping by error code try { ... } catch( SQLException ex ) { // Big Ugly Switch Statements != Object Oriented Programming. switch( ex.getErrorCode()) { case 1: ... default: // Throw an exception, perhaps? } } ... Listing Seven // Recursively unwrapping try { ... } catch( MyAPIException ex ) { try { Throwable t = ex; while( ex != null ) { t = ex.getWrappedException(); ex = ( t instanceof MyAPIException ? (MyAPIException) t : null ); } throw t; } catch( UnderlyingException e ) { // according to the underlying exception type ... } ... } Listing Eight public static void main( String[] args ) { try { if( args.length < 1 ) { System.out.println( "usage: do [it]" ); System.exit( 1 ); } } catch( IOException ex ) { // System.out isnít working, so printStackTrace() isnít any good. System.exit( 1 ); } ... } Listing Nine public static void main( String[] args ) { ... System.out.print( "Current progress: " ); if( System.out.checkError()) { // Canít communicate with the user anymore System.exit( 1 ); } ... } } Listing Ten // Unchecked exceptions // Initialization for a class that has nothing to do with I/O public void initialize() { // Load pre-computed values for this class. InputStream is = new FileInputStream( CACHE_FILE ); is.read( cache ); ... }