/* ExceptionsDemo3.java - handles the exception thrown by Scanner with a try catch block */ import java.io.*; import java.util.*; public class ExceptionsDemo3 { public static void main( String args[] ) { // Fhe following statement could throw a checked exception // since it is a checked exception we must "advertise" // the fact that we are not handling this exception in our code. // Instead we "throw" the Exception. Thus the clause // throws FileNotFoundException after the main method's signature. Scanner infile=null; String filename = args[0]; try { infile = new Scanner (new File( filename) ); } catch (FileNotFoundException fnfe ) { System.out.println("Exception caught: " + fnfe + "\n"); System.exit(0); } catch (Exception e ) // YOU SHOULD EXIT WHEN YOU CATCH A GENERAL EXCEPTION THAT DID NOT MATCH ANY EXPECTED ERROR { System.out.println("Exception other than FileNotFoundException caught: " + e + "\n"); System.exit(0); } String token = infile.next(); // read a string from infile System.out.println("1st string of the files was: " + token ); } //END main } //EOF