package finallyAndReturn;

import java.awt.FontFormatException;

public class Driver {

	public static void main(String[] args) {
		String whatWillReturn = makeMeAnError();
		// Which return value will print?
		
		System.out.println("\nWhat returns? ... " + whatWillReturn);
		
		/* It makes perfect sense that the return from the
		 * finally block will be the one that fires.  
		 * 
		 * Even though code like this will not compile, it
		 * would make sense that the first return statement is
		 * what passes control from the method to its caller
		 * 
		  	private static String doubleReturn() {
				return "One fish";
				return "Two fish";  // Cannot compile.  Unreachable code
			}
		 */
	}

	// Suppression of warning silences the 
	//    "finally block does not complete normally" error. 
	@SuppressWarnings("finally")
	private static String makeMeAnError() {
		try {
			System.out.println("Let's throw an exception");
			throw new FontFormatException("What an awful font!");
			/* Anything after here is unreachable code */
			// System.out.println("I won't compile...");
		}
		catch (FontFormatException e) {
			System.out.println("\tShould have used Calibri instead");
			return "a return from the original catch";
		}
		finally {
			// This block of code should always execute
			System.out.println("\nFinally, a decent font.");
			
			// Now our own try/catch
			try {
				System.out.println("Should we try to interrupt a thread?");
				throw new InterruptedException("Hey, wake up");
			}
			catch (InterruptedException e){
				System.out.println("\tYou are disturbing my sleep");
				return "a return from the finally";
			}
		}
	}
}
