import java.io.*;
/*============================================================== 
 * This class will create 5 exceptions and handle each sequentially.
 * No exception should cause the program to fail.
 *
 * @author Jared Gerhart
 * @version 1
 * 
//==============================================================*/
// Date:1/18/07
// Section 3
// Lab 3

    public class ExceptionHandling
   {
       /*========================================================
   	  * 
   	  *  main method - in general will only call other methods
   	  *              - required by every java application
   	  *
   	  *  @param args   command line arguments not used in this application
   	  */
   	  
       public static void main (String args[])
      {
 			// for array exception
	   	int[] numArray = {1, 2, 3};
			
			// for file not found exception
			FileReader freader;
			BufferedReader inputFile;
			
			// for number format exception
			String wrong;		// holds only letters
			int numHold;
			
			wrong = "abcde";
			numHold = 0;
			
			// for Arithmetic exception
			int valueDivide;
			valueDivide = 0;
			
			
						
			// This will cause an array exception
			try
			{
				for (int i=0; i<=3;i++)
				{
					System.out.println(numArray[i]);
				}
			}
			catch (ArrayIndexOutOfBoundsException aioobe)
			{
				System.out.println("The loop executed beyond the final index value in the array.");
			}
			
			System.out.println();
			System.out.println();
			
			
			// This will cause a file not found exception
			try
			{
				freader = new FileReader("bad.txt");
				inputFile = new BufferedReader(freader);
				System.out.println("The file was found");
			}
			catch (FileNotFoundException fnfe)
			{
				System.out.println("The file bad.txt was not found.");
			}
			
			System.out.println();
			System.out.println();
			
			
			// This will cause a number conversion exception
			try
			{
				numHold = Integer.parseInt(wrong);
			}
			catch (NumberFormatException nfe)
			{
				System.out.println("There was a conversion error: The string contained only letters");
			}
			
			System.out.println();
			System.out.println();
			
			
			// Arithmetic exception
			try
			{
				valueDivide = 1 / 0;
			}
			catch (ArithmeticException ae)
			{
				System.out.println("There was a divide by zero attempt, this cannot be performed");
			}
			
			System.out.println();
			System.out.println();
			
			System.out.println("The program is completed.");			

			    
      }
   
   }
