Chapter 8
Section 8.1
- an exception is an
object that defines an unusual or erroneous situation
- it is thrown by a
program or runtime environment, and can be caught and handled
appropriately
- Java has a predefined
set of exceptions and errors that may occur during execution of a program
– listed in Appendix K
- programs can not handle
an exception at all, handle it where it occurs, or handle it somewhere
else in the program
- if an exception isn’t
handled at all, the program terminates abnormally and produces a message
describing what exception occurred and where
- first line indicates
exception thrown and some information about why, then remaining lines are
call stack trace to show where it occurred
- listing 8.1 p.379
- to catch and handle an
exception, use try-catch block
- try identifies a block
of statements that may throw an exception
- catch clause follows
try block and defines how to handle the exception
- a try block can have
several catch clauses, each called an exception handler
- statements in a try
block are executed, then if no exception is thrown, execution continues
after try-catch block – this is normal flow that we’d like to happen
- if exception is thrown
in try block, control immediately transfers to appropriate catch clause –
control transfers to first catch clause whose exception class corresponds
to the class of the exception thrown
- after whole catch
clause is executed, control continues after try block
- listing 8.2, p. 381
- try statement can have
optional finally clause, a section of code executed no matter how try
block is exited
- finally clause is
listed after all catch clauses, and is executed after try if no exception
is thrown, or after catch if an exception is thrown
- if an exception is not
caught and handled immediately, control returns to the method that called
the method that caused the exception
- we could handle
exception there, otherwise control goes back to method that called it –
this is exception propagation – it continues until control propagates back
to main, then the program ends
- listing 8.3, p. 384
- listing 8.4, p. 385
- we can define our own
exception classes by deriving them from Exception or its descendants
- listing 8.5, 8.6 p.
388, 389
- uses throw statement to
begin exception propagation
- exceptions can be
checked or not
- checked exceptions must
be caught or listed in the throws clause of any method that may throw or
propagate it
- unchecked exceptions
are RuntimeException or any of its descendants and should not be caught or
have a throws clause
- errors are like
unchecked exceptions
Section 8.2
- a stream is an ordered
sequence of bytes
- comes from analogy that
we read from a source and write to a sink
- we treat a stream as an
input stream from which we read, or an output stream to which we write
- a particular file, for
example, can be either, but not both at the same time
- classes for streams are
divided into input and output streams, but then also into the type of
information on which they operate – character data versus byte data, and
the role they play – data streams versus processing streams
- character stream
manages 16-bit Unicode characters – it is just a long series of characters
read or written in chunks
- byte stream manages
8-byt bytes of raw binary data, typically used for reading and writing
binary data like sounds and images
- InputStream and
OutputStream classes represent byte streams
- Reader and Writer
classes represent character streams
- data stream represents
a source or destination stream, like a string in memory or a file on disk
- processing stream or
filtering stream performs manipulation on the data in a stream
- these streams are the
same classes mentioned above, just characterized differently
- IOExceptions can be
thrown during I/O operations
Section 8.3
- standard I/O streams
are System.in, System.out and System.err
- System class has three
object references, in, out and err, that are public and static as we’ve
seen
- System.in usually
represents keyboard input, while the other two map output to the monitor,
usually
- System.out and
System.err are declared to be PrintStream references, and System.In is a
generic InputStream reference
- PrintStream objects
have print and println defined already
Section 8.4
- FileReader class
represents an input file with character data that we can read from
- its constructors link
it to the program and open a stream from which data can be read
- listing 8.7, p.397
- FileWriter class
represents an output file that we can write to
- listing 8.9, p. 402
Section 8.5
- when a program ends,
data it was using is destroyed unless we store it externally, like writing
it to a file
- persistence is the concept
of objects living outside of the program where they were created
- can create persistent
objects in Java by object serialization
- transform it into a
series of bytes that represents the object, then store it into a file
- it can later be read
from the file and restored to the original object
- must implement
Serialize interface
- to serialize, call
writeObject method of an ObjectOutputStream
- to deserialize, call
readObject method of an ObjectInputStream
- if don’t want to
serialize all the information in an object, like passwords for example,
use transient when declare that variable