Chapter 8: Exceptions and I/O Streams
Chapter 8: Exceptions and I/O Streams
Chapter 8: Exceptions and I/O Streams
Ali Kassem
Ali Kassem 2
Exceptions
An exception is an object that describes an unusual or
erroneous situation
Exceptions are thrown by a program, and may be caught
and handled by another part of the program
A program can be separated into a normal execution flow
and an exception execution flow
An error is also represented as an object in Java, but
usually represents a unrecoverable situation and should not
be caught
Ali Kassem 3
Exception Handling
Java has a predefined set of exceptions and errors that can
occur during execution
A program can deal with an exception in one of three ways:
• ignore it
• handle it where it occurs
• handle it an another place in the program
Ali Kassem 4
Exception Handling
If an exception is ignored by the program, the program will
terminate abnormally and produce an appropriate message
The message includes a call stack trace that indicates the
line on which the exception occurred
The call stack trace also shows the method call trail that
lead to the attempted execution of the offending line
• The getMessage method returns a string explaining why the
exception was thrown
• The printStackTrace method prints the call stack trace
Ali Kassem 5
The try Statement
To process an exception when it occurs, the line that throws
the exception is executed within a try block
A try block is followed by one or more catch clauses, which
contain code to process an exception
Each catch clause has an associated exception type and is
called an exception handler
When an exception occurs, processing continues at the first
catch clause that matches the exception type
See ProductCodes.java (page 451)
Ali Kassem 6
The finally Clause
A try statement can have an optional clause following the
catch clauses, designated by the reserved word finally
The statements in the finally clause always are executed
Ali Kassem 7
Exception Propagation
An exception can be handled at a higher level if it is not
appropriate to handle it where it occurs
Exceptions propagate up through the method calling
hierarchy until they are caught and handled or until they
reach the level of the main method
A try block that contains a call to a method in which an
exception is thrown can be used to catch that exception
See Propagation.java (page 455)
See ExceptionScope.java (page 456)
Ali Kassem 8
The throw Statement
A programmer can define an exception by extending the
Exception class or one of its descendants
Exceptions are thrown using the throw statement
Ali Kassem 9
Checked Exceptions
An exception is either checked or unchecked
Ali Kassem 10
Unchecked Exceptions
An unchecked exception does not require explicit handling,
though it could be processed that way
The only unchecked exceptions in Java are objects of type
RuntimeException or any of its descendants
Errors are similar to RuntimeException and its
descendants
• Errors should not be caught
Data Processing
Streams Streams
Input Streams
Output Streams
Character
Streams
Byte
Streams
Character vs. Byte Streams
A character stream manages 16-bit Unicode characters
• Typically they are used to read and write sounds and images
InputStreamReader isr =
new InputStreamReader (System.in)
BufferedReader stdin = new BufferedReader (isr);