Source of CatchExceptions.java


  1: package exceptions;

  3: import java.util.NoSuchElementException;
  4: import java.util.InputMismatchException;
  5: import java.io.IOException;
  6: import java.io.FileNotFoundException;
  7: import csci2341.CodePresenter;
  8: import csci2341.Utilities;

 10: /**
 11:  * A program to demonstrate catch blocks.
 12:  *
 13:  * @author Mark Young (A00000000)
 14:  */
 15: public class CatchExceptions {

 17:     public static void main(String[] args) 
 18:             throws Exception {
 19:         CodePresenter myCode 
 20:                 = new CodePresenter("exceptions/CatchExceptions.java");
 21:         // part 1
 22:         System.out.println("One catch block");
 23:         myCode.printMyCode("// 1", "// 2");
 24:         // 1
 25:         try {
 26:             RandomThrower.randomThrow();
 27:         } catch (NoSuchElementException nse) {
 28:             System.out.println("Caught a " + nse);
 29:         }
 30:         // 2
 31:         Utilities.pause();

 33:         // part 2
 34:         System.out.println("Multiple catch blocks");
 35:         RandomThrower.add(new InputMismatchException());
 36:         RandomThrower.add(new NoSuchElementException());
 37:         RandomThrower.add(new ArrayIndexOutOfBoundsException());
 38:         myCode.printMyCode("// 3", "// 4");
 39:         // 3
 40:         for (int i = 1; i <= 4; ++i) {
 41:             try {
 42:                 RandomThrower.randomThrow();
 43:             } catch (InputMismatchException ime) {
 44:                 System.out.println("Caught a " + ime);
 45:                 System.out.println("In the first catch block.");
 46:             } catch (NoSuchElementException nse) {
 47:                 System.out.println("Caught a " + nse);
 48:                 System.out.println("In the second catch block.");
 49:             } catch (ArrayIndexOutOfBoundsException aioob) {
 50:                 System.out.println("Caught a " + aioob);
 51:                 System.out.println("In the third catch block");
 52:             }
 53:         }
 54:         // 4
 55:         Utilities.pause();

 57:         System.out.println("Combined catch blocks");
 58:         RandomThrower.add(new FileNotFoundException());
 59:         RandomThrower.add(new IOException());
 60:         myCode.printMyCode("// 5", "// 6");
 61:         // 5
 62:         for (int i = 1; i <= 4; ++i) {
 63:             try {
 64:                 RandomThrower.randomThrow();
 65:             } catch (InputMismatchException | FileNotFoundException exc) {
 66:                 System.out.println("Caught a " + exc);
 67:                 System.out.println("In the first catch block.");
 68:             } catch (IOException ime) {
 69:                 System.out.println("Caught a " + ime);
 70:                 System.out.println("In the second catch block.");
 71:             } catch (NoSuchElementException 
 72:                     | ArrayIndexOutOfBoundsException exc) {
 73:                 System.out.println("Caught a " + exc);
 74:                 System.out.println("In the third catch block.");
 75:             }
 76:         }
 77:         // 6
 78:         Utilities.pause();
 79:     }

 81: }