import java.io.IOException;
import java.util.Random;

/**
 * An example with the finally clause, to show why it's necessary. This is as
 * opposed to just having code after the try/catch
 * 
 * @author CSCI209
 */
public class AnotherFinallyTest {

	private static final Random randomNumberGenerator = new Random();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		handlesExceptions();
	}

	public static void handlesExceptions() {
		try {
			throwsExceptions();
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Problem happened, but I caught it!");
		} finally {
			System.out.println("In Finally");
		}

		// this only executes if the above code "went okay"
		// i.e., only if either the try executed successfully or
		// the catch executed.
		// If something "escaped" the try/catch, then we won't reach here.
		System.out.println("After the try/catch");

	}

	public static void throwsExceptions() {
		int whichException = randomNumberGenerator.nextInt(3);

		switch (whichException) {
		case 0:
			System.out.println("ArrayIndexOutOfBoundsException - will be caught/handled");
			throw new ArrayIndexOutOfBoundsException();
		case 1:
			System.out.println("IllegalArgumentException - won't be caught/handled");
			throw new IllegalArgumentException();
		default:
			System.out.println("No problems!!");
		}

	}
}
