// This is the original program showing correct use of try catch block import java.util.Scanner; import java.io.*; public class ReadFromFile { public static void main (String [] args) { String fileName; Scanner fileScanner; Scanner keyboard; int value; keyboard = new Scanner (System.in); System.out.println (" Please enter name of file holding integers and hit return "); fileName = keyboard.nextLine(); System.out.println (" The name of the file you want to open is " + fileName); try { fileScanner = new Scanner (new File (fileName)); while (fileScanner.hasNextInt()) { value = fileScanner.nextInt(); System.out.println (" The value you got from the file is " + value); } } catch (FileNotFoundException fnfe) { System.out.println (" The file you wanted to open was not found "); } } // end main } // end class _____________________________________________________ // This version has no try catch block and requires the "throws IOException" to compile import java.util.Scanner; import java.io.*; public class ReadFromFile2 { public static void main (String [] args) throws IOException { String fileName; Scanner fileScanner; Scanner keyboard; int value; keyboard = new Scanner (System.in); System.out.println (" Please enter name of file holding integers and hit return "); fileName = keyboard.nextLine(); System.out.println (" The name of the file you want to open is " + fileName); fileScanner = new Scanner (new File (fileName)); while (fileScanner.hasNextInt()) { value = fileScanner.nextInt(); System.out.println (" The value you got from the file is " + value); } } // end main } // end class ____________________________________________________ // This file uses "grandaddy Exception e" import java.util.Scanner; import java.io.*; public class ReadFromFile3 { public static void main (String [] args) { String fileName; Scanner fileScanner; Scanner keyboard; int value; keyboard = new Scanner (System.in); System.out.println (" Please enter name of file holding integers and hit return "); fileName = keyboard.nextLine(); System.out.println (" The name of the file you want to open is " + fileName); try { fileScanner = new Scanner (new File (fileName)); while (fileScanner.hasNextInt()) { value = fileScanner.nextInt(); System.out.println (" The value you got from the file is " + value); } } catch (Exception e) { System.out.println (" The file you wanted to open was not found "); } } // end main } // end class ___________________________________________________ // This file uses "grandaddy e" but calls getMessage to determine what the exception was import java.util.Scanner; import java.io.*; public class ReadFromFile4 { public static void main (String [] args) { String fileName; Scanner fileScanner; Scanner keyboard; int value; keyboard = new Scanner (System.in); System.out.println (" Please enter name of file holding integers and hit return "); fileName = keyboard.nextLine(); System.out.println (" The name of the file you want to open is " + fileName); try { fileScanner = new Scanner (new File (fileName)); while (fileScanner.hasNextInt()) { value = fileScanner.nextInt(); System.out.println (" The value you got from the file is " + value); } } catch (Exception e) { System.out.println (" The file you wanted to open was not found "); System.out.println(e.getMessage()); } } // end main } // end class ___________________________________________________ // This version has the wrong catch block and requires "throws IOException" to compile import java.util.Scanner; import java.io.*; public class ReadFromFile5 { public static void main (String [] args)throws IOException { String fileName; Scanner fileScanner; Scanner keyboard; int value; keyboard = new Scanner (System.in); System.out.println (" Please enter name of file holding integers and hit return "); fileName = keyboard.nextLine(); System.out.println (" The name of the file you want to open is " + fileName); try { fileScanner = new Scanner (new File (fileName)); while (fileScanner.hasNextInt()) { value = fileScanner.nextInt(); System.out.println (" The value you got from the file is " + value); } } catch (ArrayIndexOutOfBoundsException aioobe) { System.out.println (" The file you wanted to open was not found "); } } // end main } // end class ___________________________________________________ // This version has two catch blocks, the needed one and an extra import java.util.Scanner; import java.io.*; public class ReadFromFile6 { public static void main (String [] args) { String fileName; Scanner fileScanner; Scanner keyboard; int value; keyboard = new Scanner (System.in); System.out.println (" Please enter name of file holding integers and hit return "); fileName = keyboard.nextLine(); System.out.println (" The name of the file you want to open is " + fileName); try { fileScanner = new Scanner (new File (fileName)); while (fileScanner.hasNextInt()) { value = fileScanner.nextInt(); System.out.println (" The value you got from the file is " + value); } } catch (ArrayIndexOutOfBoundsException aioobe) { System.out.println ("Array index went out of bounds "); } catch (FileNotFoundException fnfe) { System.out.println (" The file you wanted to open was not found "); } } // end main } // end class