import java.util.NoSuchElementException;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileNotFoundException;
import csci2341.CodePresenter;
import csci2341.Utilities;

/**
 * A program to demonstrate catch blocks.
 *
 * @author Mark Young (A00000000)
 */
public class CatchExceptions {

    public static void main(String[] args) 
            throws Exception {
        CodePresenter myCode 
                = new CodePresenter("exceptions/CatchExceptions.java");
        // part 1
        System.out.println("One catch block");
        myCode.printMyCode("// 1", "// 2");
        // 1
        try {
            RandomThrower.randomThrow();
        } catch (NoSuchElementException nse) {
            System.out.println("Caught a " + nse);
        }
        // 2
        Utilities.pause();

        // part 2
        System.out.println("Multiple catch blocks");
        RandomThrower.add(new InputMismatchException());
        RandomThrower.add(new NoSuchElementException());
        RandomThrower.add(new ArrayIndexOutOfBoundsException());
        myCode.printMyCode("// 3", "// 4");
        // 3
        for (int i = 1; i <= 4; ++i) {
            try {
                RandomThrower.randomThrow();
            } catch (InputMismatchException ime) {
                System.out.println("Caught a " + ime);
                System.out.println("In the first catch block.");
            } catch (NoSuchElementException nse) {
                System.out.println("Caught a " + nse);
                System.out.println("In the second catch block.");
            } catch (ArrayIndexOutOfBoundsException aioob) {
                System.out.println("Caught a " + aioob);
                System.out.println("In the third catch block");
            }
        }
        // 4
        Utilities.pause();

        System.out.println("Combined catch blocks");
        RandomThrower.add(new FileNotFoundException());
        RandomThrower.add(new IOException());
        myCode.printMyCode("// 5", "// 6");
        // 5
        for (int i = 1; i <= 4; ++i) {
            try {
                RandomThrower.randomThrow();
            } catch (InputMismatchException | FileNotFoundException exc) {
                System.out.println("Caught a " + exc);
                System.out.println("In the first catch block.");
            } catch (IOException ime) {
                System.out.println("Caught a " + ime);
                System.out.println("In the second catch block.");
            } catch (NoSuchElementException 
                    | ArrayIndexOutOfBoundsException exc) {
                System.out.println("Caught a " + exc);
                System.out.println("In the third catch block.");
            }
        }
        // 6
        Utilities.pause();
    }

}
