Source of MoreCrashes.java


  1: import java.util.Scanner;
  2: import java.util.NoSuchElementException;

  4: /**
  5:  * A program that crashes a lot.
  6:  *
  7:  * NOTE: It's not normal (or a good idea) to use printStackTrace() in a 
  8:  * finished program. The person using your program doesn't want or need to see
  9:  * that kind of internal gobble-de-gook! I'm using it here only to help you as
 10:  * a programmer recognize the information it prints out, as it is the sort of
 11:  * thing you'll run into when you're dealing with your own code crashing.
 12:  *
 13:  * @author Mark Young (A00000000)
 14:  */
 15: public class MoreCrashes {

 17:     public static void main(String[] args) {
 18:         System.out.println("\n\n"
 19:                 + "This program crashes a lot!\n");

 21:         // try to set array elements that don't exist
 22:         try {
 23:             int[] numbers = new int[2];
 24:             numbers[0] = 1;
 25:             numbers[1] = 2;
 26:             numbers[2] = 3;
 27:             System.out.println(numbers[0] + ", " + numbers[1]
 28:                     + ", " + numbers[2]);
 29:         } catch (ArrayIndexOutOfBoundsException e) {
 30:             System.out.println("That array index was out of bounds!");
 31:             e.printStackTrace();
 32:             System.out.println("\nBut we'll try to carry on!\n");
 33:         }

 35:         // speak to something that's not there
 36:         try {
 37:             String name = null;
 38:             System.out.println("Here is your name: " + name);
 39:             System.out.println("It has " + name.length() + " letters in it.");
 40:             System.out.println("Its first letter is " + name.charAt(0));
 41:         } catch (NullPointerException e) {
 42:             System.out.println("That String was null!");
 43:             e.printStackTrace();
 44:             System.out.println("\nBut we'll still try to carry on!\n");
 45:         }

 47:         // run out of data in the middle
 48:         try {
 49:             Scanner kbd = new Scanner(System.in);
 50:             System.out.println("Enter four numbers here: ");
 51:             Scanner line = new Scanner(kbd.nextLine());
 52:             for (int i = 1; i <= 5; i++) {
 53:                 System.out.println("The " + i + "th number you entered was "
 54:                         + line.nextInt());
 55:             }
 56:         } catch (NoSuchElementException e) {
 57:             System.out.println("Dang!  Out of numbers!");
 58:             System.out.println("Didn't I tell you I wanted five numbers?");
 59:             System.out.println(".");
 60:             System.out.println(".");
 61:             System.out.println(".");
 62:             System.out.println("Oops.  Guess I said four.  My mistake.");
 63:             System.out.println();
 64:             System.out.println("Anyway, here's where the exception came from:");
 65:             e.printStackTrace();
 66:             System.out.println("\nBye!\n");
 67:             System.exit(1);
 68:         }
 69:     }

 71: }