   import java.util.*; //imports util classes
   import java.io.*; //imports I/O classes
/*****************************************************
* This program executes blocks of code with exceptions
* and catches the exception
*
* @author - Ben Rodes
* @version V1 - 1/17/07
******************************************************/
//Ben Rodes
//2:30 Lab
//1/17/07
//Lab 3
    public class ExceptionHandler
   {
   /*****************************************************
   * Executes blocks of code with exceptions and
   * catches the exception
   *
   * @param args Command line arguments (not used)
   *
   * @exception ArrayIndexOutOfBoundsException
   * @exception FileNotFoundException
   * @exception NumberFormatException
   * @exception InputMismatchException
   * @exception StringIndexOutOfBoundsException
   *****************************************************/
       public static void main(String[] args)
      {
      //declarations to throw different exceptions
      
         int[] array = {1, 2, 3};
      
         Scanner fileScanner;  
         Scanner keyboard; 		
      
         int num;		
      
         char letter; 		
      
         String message; 		
      
      //action code
      
      //Prints out contents of array and goes out of bounds
      
         try
         {
            for(int count = 0; count <= 3; count++)
               System.out.println(array[count]);
         }
         
         //catches exception when the loop goes out of bounds of the array
         
             catch (ArrayIndexOutOfBoundsException aioobe)
            {
               System.out.println("Array out of bounds.  The array variable " +
                  "has a length of " + array.length);
            }
      
         System.out.println(); 
      
      //checks to see if junk.txt exists 
      
         try
         {
            fileScanner = new Scanner (new File ("junk.txt"));  //instantiates the fileScanner
         }
         
         //if the file doesn't exist, a message is printed out
         
             catch (FileNotFoundException fnfe)
            {
               System.out.println("junk.txt not found");  //message of failure
            }
      	
      
         System.out.println(); 
      
      //Initializes the string "message" and num variable, 
      //and trys to convert the string "message" to an integer
      
         message = "Hello there";
      	
         try
         {
            num = Integer.parseInt(message); 
         }
         
         //if the string is not an integer, a message is printed
         
             catch (NumberFormatException nfe)
            {
               System.out.println("The message \"" + message + "\" is not an integer");
            }
      
         System.out.println(); 
      
         keyboard = new Scanner(System.in);
      
      //sets num to 0. Also takes input and determines if it is an integer value
      
         num = 0;  
      
         try
         {
            System.out.print("Type a non-integer number to throw the exception: ");
            num = keyboard.nextInt();
            System.out.println();
            System.out.println(num + " is an integer.  No catch generated.");
         
         }
         
         //prints a message if the input is not an integer
         
             catch (InputMismatchException ime)
            {
               message = keyboard.nextLine();
               System.out.println("The value " + message + " is not an integer.");
            }
      
         System.out.println();
      
      //Sets "message" to a value of length 3 and searches for a char outside of that length
      
         message = "123";  
      
         try
         {
            letter = message.charAt(22);
         }
         
         //prints out a message if there is no charAt(22)
         
             catch (StringIndexOutOfBoundsException sioobe)
            {
               System.out.println("The string index is out of bounds.  The string has a length of 3");
            }
            
         System.out.println();
            
      	//prints a message if the program runs all the way thru
      	
         System.out.println("This program has ended successfully.");
      }
   }

