Java Reference
In-Depth Information
to
main( )
. Now you can understand why. An input statement might generate an
IOExcep-
tion
, and at that time, we weren't able to handle that exception. Thus, such an exception
would be thrown out of
main( )
and needed to be specified as such. Now that you know
about exceptions, you can easily handle
IOException
.
Let's look at an example that handles
IOException
. It creates a method called
prompt(
)
, which displays a prompting message and then reads a character from the keyboard. Since
input is being performed, an
IOException
might occur. However, the
prompt( )
method
does not handle
IOException
itself. Instead, it uses a
throws
clause, which means that the
calling method must handle it. In this example, the calling method is
main( )
, and it deals
with the error.
On a related point, notice that
IOException
is fully qualified by its package name
java.io
. As you will learn in
Chapter 10
, Java's I/O system is contained in the
java.io
pack-
age. Thus, the
IOException
is also contained there. It would also have been possible to
import
java.io
and then refer to
IOException
directly.
Three Recently Added Exception Features