An Exception
is thrown.
Specifically, an InputMismatchException
is thrown.
This chapter shows how to use the try/catch/finally
structure to handle Exceptions
that happen when doing I/O.
Here is how to construct a Scanner
object that connects to
a disk file:
// create a File object File file = new File("myData.txt"); // connect a Scanner to the file Scanner scan = new Scanner( file );
These statements first create a File
object.
This is a software object that represents a file name.
Creating a File
object does not create a disk file.
Next a Scanner
object is constructed and connected
to the actual file.
If the file myData.txt does not exist in the current disk directory,
or if there are other problems opening it,
the Scanner
constructor will throw an FileNotFoundException
,
a child class of IOException
.
Recall that IOException
s are checked Exception
s.
What must a method do if it might cause a checked Exception
?