Java Reference
In-Depth Information
these problems by using exception throwing. This provides a clearly defined way for an object
to report to a client that something has gone wrong. Exceptions prevent a client from simply
ignoring the problem, and encourage programmers to try to find an alternative course of action
as a workaround if something does go wrong.
When developing a class, assert statements can be used to provide internal consistency check-
ing. These are typically omitted from production code.
Input/output is an area where exceptions are likely to occur. This is primarily because a pro-
grammer has little, if any, control over the environments in which their programs are run, but it
also reflects the complexity and diversity of the environments in which programs are run.
The Java API supports input/output of both textual and binary data via readers, writers, and
streams. Java 7 has brought changes to I/O through the introduction of the java.nio packages.
Exercise 12.54 Add to the address-book project the ability to store multiple e-mail
addresses in a ContactDetails object. All of these e-mail addresses should be valid keys.
Use assertions and JUnit testing through all stages of this process to provide maximum confi-
dence in the final version.
Terms introduced in this chapter
exception, unchecked exception, checked exception, exception handler, assertion,
Concept summary
exception An exception is an object representing details of a program failure. An exception is
thrown to indicate that a failure has occurred.
unchecked exception An unchecked exception is a type of exception whose use will not
require checks from the compiler.
checked exception A checked exception is a type of exception whose use will require extra
checks from the compiler. In particular, checked exceptions in Java require the use of throws
clauses and try statements.
exception handler Program code that protects statements in which an exception might be
thrown is called an exception handler . It provides reporting and/or recovery code should one
assertion An assertion is a statement of a fact that should be true in normal program execu-
tion. We can use assertions to state our assumptions explicitly and to detect programming
errors more easily.
serialization Serialization allows whole objects, and object hierarchies, to be read and writ-
ten in a single operation. Every object involved must be from a class that implements the
Serializable interface.
Search WWH ::

Custom Search