import java.io.*;
import java.util.Scanner;
/***********************************************
 * 5 exceptions will be created and handled using the try catch blocks
 *
 * @author - Eric Hartway
 * @version - V1 - 1/17/07
 ************************************************/
public class Exception
{
	/********************************************
	 * gives 5 examples of exceptions and handles them
	 *
	 * 1.ArrayIndexOutOfBoundsException 
	 * 2.NumberFormatException
	 * 3.FileNotFoundException
	 * 4.ArithmeticException
	 * 5.ArrayStoreException
	 *
	 * @param arg unused
	 *********************************************/
	public static void main(String args[])
	{
		Scanner kb;
		kb = new Scanner(System.in);
		
		int[] array = {1, 2, 3};
			
		String[] array2 = {"1", "2", "a","3"};
			
		FileReader file1;
		BufferedReader buff1;
		String fileName;
		fileName = null;
		
		int num;
		
		int num2;		
	
	
	
		try  //thows an out of bounds exception
		{
			for (int i = 0; i <= array.length; i++)  //element number starts at zero, not one
				System.out.println(array[i]);
		}
		catch (ArrayIndexOutOfBoundsException aioob)
		{
			System.out.println("The array index is out of bounds");
		}
		
		
		
		System.out.println();
		
		
		
		
		try  //throws a number format exception because of the "a" in the array
		{
			for (int i = 0; i < array2.length; i++)
				System.out.println(Integer.parseInt(array2[i]));
		}
		catch(NumberFormatException nfe)
		{
			System.out.println("The format of the array is incorrect");
		}
		
		
		
		System.out.println();
		
		
		
		
		
		try  //throws an exception if the file inputed by user is not found
		{
			System.out.print("Type the name of a file ");
			fileName = kb.nextLine();
						
			file1 = new FileReader(fileName);
			buff1 = new BufferedReader(file1);
			System.out.println("File " + fileName +" was found");
		}
		catch (FileNotFoundException fnfe)
		{
			System.out.println("The file " + fileName + " was not found");
		}
		
		
		
		System.out.println();
		
		
		
		
		
		try //throws an arithmetic axception by attempting to divide a given number by zero
		{
			System.out.print("Type a number ");
			num2 = kb.nextInt();
		
			System.out.println(num2/0);  //can not divide be zero with any number
		}
		catch (ArithmeticException ae)
		{
			System.out.println("No number can be divided by zero");
		}
		
		
		
		
		System.out.println();
		
		
				
		
		try //attempts to store an integer in a String array
		{		
			Object x[] = new String[3];
         x[0] = new Integer(0);
			System.out.println(x[0]); // these types are incompatable
		}
		catch (ArrayStoreException ase)
		{
			System.out.println("Can not store integer input into a String array");
		}
		
		
		
		
		System.out.println();
		
		
		
		System.out.println("The program has finished"); 



	}
}
			
			
