A program
often encounters problems as it executes.
It may have trouble reading data,
there might be illegal characters in the data,
or an array index might go out of bounds.
The Java Exception
class
enables you to
deal with such problems.
Input and output are especially error prone. Exception handling is essential for I/O programming, the topic of several chapters following.
try
, catch
, and finally
statementsInspect the following. What went wrong?
C:\JavaSource>java Square Enter an integer: rats Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Square.main(Square.java:12)