class TooSmallException extends Exception{}
class TooBigException extends Exception{}

public class ExceptionExercises {
	
	// Q1) What does this method output?
	public static void example1() {
		System.out.println("start of example1");
		int[] array = {1, 2, 3};
		
		try {
			System.out.println(array[9]);
			System.out.println("done with array");
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Could not access array at that index");
			System.out.println(e);
		}
		System.out.println("end of example1");
	}
	
	// Q2) Calling custom exceptions
	public static void example2() {
		int a = 5;
		int b = 1000;
		
		try {
			onlySmallNumbers(a);
			//onlySmallNumbers(b);
			//onlyLargeNumbers(a);
			
			System.out.println("here");
		} catch	(TooBigException e) {
			System.out.println("caught " + e + " in example2 method");
		} 
		
		System.out.println("end of example2");
	}
	
	// causes the program to crash if x > 10
	public static void onlySmallNumbers(int x) throws TooBigException {
		System.out.println("begin small numbers method with " + x);
		
		if (x > 10) { // when given a number that is too big --> CRASH
			throw new TooBigException();
		}

		try {
			onlyLargeNumbers(x);
		} catch (TooSmallException e) {
			System.out.println("caught " + e + " in onlySmallNumbers method");
		}
		
		System.out.println("end small numbers method with " + x);
	}
	
	// causes the program to crash if x < 100
	public static void onlyLargeNumbers(int x) throws TooSmallException {
		System.out.println("begin large numbers method with " + x);
		if (x < 100) {
			throw new TooSmallException();
		}
		System.out.println("end large numbers method with " + x);
	}
	
	
	public static void main(String[] args) throws TooBigException {
		
		/* Complete the following exercises,
		    one at a time */
			
		//example1();
		example2();
		System.out.println("end of main");
	}
}
