Page 590 - Introduction to Programming with Java: A Problem Solving Approach
P. 590
556 Chapter 14 Exception Handling 14.1 Introduction
As you know, programs sometimes generate errors. Compile-time errors deal with incorrect syntax, like forgetting parentheses around an if statement condition. Runtime errors deal with code that behaves inap- propriately, like trying to divide by zero. In previous chapters, we fixed compile-time errors by correcting the erroneous syntax, and we fixed runtime errors by making code more robust. In this chapter, we deal with errors using a different technique—exception handling. We’ll describe exceptions more formally later on, but for now, think of an exception as an error, or simply something that goes wrong with a program. Exception handling is an elegant way to deal with such problems.
We start this chapter by looking at a common problem—making sure that users enter a valid number when they are asked for a numeric input. You’ll learn how to implement such input validation using try and catch blocks, two of the key exception handling constructs. There are different types of exceptions, and you’ll learn how to deal with the different types appropriately. In the chapter’s final section, you’ll use exception handling as part of a GUI line-plot program.
To understand this chapter, you need to be familiar with object-oriented programming, arrays, and inheritance basics. As such, you need to have read up through Chapter 12. This chapter does not depend on material covered in Chapter 13.
We realize that readers may want to read different amounts of this chapter (Exception Handling) and
the next chapter (Files). If you plan to read the next chapter, then you’ll need to read this chapter in its en-
tirety since the topic addressed in the next chapter, file manipulation, relies heavily on exception handling.
On the other hand, if you plan to skip the next chapter and go directly to Chapters 16 and 17 (GUI program-
ming), then you’ll need to read only the first part of this chapter, Sections 14.1 through 14.7.
Apago PDF Enhancer
14.2 Overview of Exceptions and Exception Messages
As defined by Sun,1 an exception is an event that disrupts the normal flow of instructions during the execu- tion of a program. Exception handling is a technique for handling such exceptions gracefully.
The first exceptions we’ll look at deal with invalid user input. Have you ever crashed a program (made it terminate ungracefully) due to invalid input? If a program calls the Scanner class’s nextInt method and a user enters a non-integer, the Java Virtual Machine (JVM) generates an exception, displays a big, ugly error message, and terminates the program. Here’s a sample session that illustrates what we’re talking about:
an exception
Enter an integer: 45.6
⎫ at java.util.Scanner.throwFor(Scanner.java:819) ⎪ ⎪ ⎬ ⎪ at java.util.Scanner.nextInt(Scanner.java:2000) ⎪ ⎭
at Test.main(Test.java:11)
1 Sun Microsystems, “The Java Tutorial, Handling Errors with Exceptions,” which can be found on the Internet at http://java.sun.com/docs/books/tutorial/essential/exceptions/.
u
u
s
s
e
e
r
r
i
in
n
p
pu
u
t
t
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
exception message