Uilscanner importjavaiofile

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: le) throw new FileNotFoundExcepIon(…); // excepIon object … everything ok, keep doing stuff } •  Throwing an exception is a special form of control flow –  Causes method to terminate –  Passing the exception on to the calling method –  The calling method can handle the exception, or pass it on CPSC 324 ‐‐ Spring 2010 7 Scanner example import java.uIl.Scanner; import java.io.File; import java.io.FileNotFoundExcep,on; A quick solution public class ScannerTest { public staIc void main(String args) throws FileNotFoundExcepIon { File aFile = new File(“file.txt ”); Scanner in = new Scanner(aFile); while(in.hasNextLine()) System.out.println(in.nextLine()); in.close(); } Here we are “passing on” the exception } CPSC 324 ‐‐ Spring 2010 8 4 1/28/10 Scanner example But, what happens if import java.uIl.Scanner; file.txt is not found? import java.io.File; import java.io.FileNotFoundException; public class ScannerTest { public staIc void main(String args) throws...
View Full Document

Ask a homework question - tutors are online