   import java.io.*;
   import java.util.Scanner;
/**============================================================== 
 * Exceptions are conditions in a program that 
 *lead to a run time error. The default exception handler 
 *processes the exception and produces documentation of where the 
 *exception occurred and the exceptional condition. in this program we will work with 
 *catching an exception and handling it using Java's try/catch structure.
 *
 * @author  Toufic Arabi
 * @version  1
 * 
//==============================================================  */
// Date: 01/17/2006 
// Section 3
// Lab 3 

    public class exceptionsTesting
   {
       /** ========================================================
   	  * 
   	  *	main method -there are 5 different "blocks" of code. 
   	  *Each block should lead to one exception, then catch and handle 
   	  *that exception. To handle the exception, the block will print an appropriate 
   	  *message indicating the appropriate data (echo the values) and the kind of 
   	  *exception generated.		  *
   	  *@param args   command line arguments not used in this application
   	  */
   	  
       public static void main (String args[])
      {
      	 
         int number,  num;
         int [] array = {1, 2, 3, 4, 5};// creates an array of 5 integer elements
         char character;
      	
         String toufic;
         FileReader freader;// creates a FileReader object
         Scanner Keyboard;
      	
         num = 0;
         character = 'q';
         toufic = "Toufic"; 
      	
      	//FileNotFoundException      
         try 
         {
            System.out.print("the file you are trying to open is File.TXT\n");
            
          	//opens the file  
            freader = new FileReader("File.TXT");	
         }
         
             catch (FileNotFoundException fnfe)
            {
               System.out.print("The file File.TXT was not found.\n");
               System.out.println(fnfe.getMessage());
               System.out.print("\n");
            }
      	
      	//ArrayIndexOutOfBoundsException
         try  
         {
            System.out.print("the array has the following 5 elements:");
            for (int count = 0; count < array.length; count++)
            {
               System.out.print(array[count] + " " );
            	
            }
         	
            System.out.print("\n");
            System.out.print("Trying to access the 6th element of the array\n");
            System.out.print(array[5]);	
         
         }
         
             catch (ArrayIndexOutOfBoundsException aioobe)
            {
               System.out.print("The array doesn't include a 6th element\n");
               System.out.print(aioobe.getMessage());
               System.out.print("\n\n");	
            }
      	
      	//NumberFormatException
         try
         {
            System.out.print("you are tring to convert the string Toufic to an integer.\n");
            number = Integer.parseInt("Toufic");
         		
         }
         
             catch (NumberFormatException nfe)
            {
               System.out.print("Non Numeric data entered\n");
               System.out.print(nfe.getMessage());
               System.out.print("\n\n");
            }
      	
      	//StringIndexOutOfBoundsException
         try
         {	
            System.out.print("the String is Toufic\n");
            System.out.print("Looking for 7th character in the String Toufic\n");
         	
            character = toufic.charAt(6); 
         	
         }
         
             catch (StringIndexOutOfBoundsException sioobe)
            {
               System.out.print("the character you are looking for does not exist in " +
                  			  toufic + "\n");
               System.out.print(sioobe.getMessage());
               System.out.print("\n\n");
            }
         
      	//ArithmeticException    
         try
         {
            Keyboard = new Scanner(System.in);
            System.out.print("Enter an integer and hit return:");
         	
            num = Keyboard.nextInt();
            System.out.print("we will try to divide that number by 0\n");
            num = num / 0;
            
         }
         
             catch (ArithmeticException ae)
            {
               System.out.print("the number you entered is " + num + "\n");
               System.out.print("you can not divide " + num + " by zero\n");
               System.out.print(ae.getMessage());
               System.out.print("\n\n");
            }
         
         finally 
         {
            System.out.print("The demonstration of 5 different types of" +
               				 " exceptions has ended\n");
            System.out.print("The program terminated properly.");
         }
      } //end main
   
   }
