Source of CatchExceptions.java


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

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

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

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

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

 79: }