created: 04/27/00; revised: 07/06/02, 06/07/03, 07/20/06
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 classes Error and Exception
enable programmers to
deal with such problems.
Using them, you can write programs that
recover from errors and keep on running.
This is important.
A word processor program should not crash
when the user makes an error!
try, catch, and finally statementsInput and output is especially error prone. Exception handling is essential for I/O programming, the topic of the next several chapters.
Inspect the following. What went wrong?
C:/cs151/chap80>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)
C:/cs151/chap80>