lecture21.ppt
Download
Report
Transcript lecture21.ppt
CS110- Lecture 21
May 9, 2005
Agenda
Exceptions
Enumerated types
Iterators – Reading Files
Iterators – foreach loop
Quiz
6/30/2016
CS110-Spring 2005, Lecture 21
1
Failure
Exception: occurrence of a detectable,
abnormal situation which may lead to system
failure.
Can design programs so that program itself
will detect some logical errors.
6/30/2016
CS110-Spring 2005, Lecture 21
2
The Java exception mechanism
Allows for detecting, reporting, and handling
exceptions.
Java detects certain run-time errors, such as:
attempts to divide by zero
use of a null reference when an object is
required.
System notifies program of error by throwing an
exception from point at which error occurred.
6/30/2016
CS110-Spring 2005, Lecture 21
3
The Java exception mechanism
A thrown exception involves transfer of
control:
processor stops executing current sequence of
statements, and
begins executing statements at a different point in
program.
Exception is caught or handled at the point to
which control is transferred.
6/30/2016
CS110-Spring 2005, Lecture 21
4
The Java exception mechanism
6/30/2016
CS110-Spring 2005, Lecture 21
5
The Java exception mechanism
A few standard exceptions which are
subclasses of RuntimeException:
ArithmeticException: an exceptional arithmetic situation has arisen, such as an
integer division with zero divisor.
ClassCastException: attempt made to cast reference to an inappropriate type.
IllegalArgumentException: method invoked with invalid or inappropriate
argument, or inappropriate object.
NullPointerException: attempt to use a null reference in case where an object
reference was required.
6/30/2016
CS110-Spring 2005, Lecture 21
6
Catching exceptions
Syntactic code structure for the detection and handling
of errors.
try {
statements
:
} catch ( exception parameter1 ) {
statements
:
} catch ( exception parameter2 ) {
statements
:
Block where exceptions
may be raised
Blocks to catch
exceptions
} …
6/30/2016
CS110-Spring 2005, Lecture 21
7
Catching exceptions
Exceptions are objects
try {
statements
} catch ( ArithmeticExection e) {
statements
:
} catch ( NullPointerException e ) {
statements
:
} catch ( Exception e ) {
statements
:
}
6/30/2016
CS110-Spring 2005, Lecture 21
8
Catching exceptions
Processor first performs statements of the try
block.
If no exceptions occur, try-catch is complete,
and catch ignores clauses.
If an exception is thrown during execution of
try block, an attempt is made to match
exception to catch clause parameters.
6/30/2016
CS110-Spring 2005, Lecture 21
9
Catching exceptions
try {
i = 5/j;
name = s.name();
} catch (ArithmeticException e) {
System.out.println(“Divide by zero error”);
} catch (NullPointerException e) {
System.out.println(“ Null pointer error”);
}
6/30/2016
CS110-Spring 2005, Lecture 21
10
Propagated exceptions
A not caught exception:
Exception is thrown by execution of
statement not part of a try-catch,
or
Thrown exception does not match any catch
clauses.
6/30/2016
CS110-Spring 2005, Lecture 21
11
Propagated exceptions
A not caught exception is propagated
up the “call chain”.
If no method in call chain catches
exception, program terminates with error
message.
6/30/2016
CS110-Spring 2005, Lecture 21
12
Enumerated Types
Java allows you to define an
enumerated type, which can then be
used to declare variables
An enumerated type establishes all
possible values for a variable of that
type
The values are identifiers of your own
choosing
6/30/2016
CS110-Spring 2005, Lecture 21
13
Enumerated Types
The following declaration creates an
enumerated type called Season
enum Season {winter, spring,
summer, fall};
Any number of values can be listed
6/30/2016
CS110-Spring 2005, Lecture 21
14
Enumerated Types
Once a type is defined, a variable of
that type can be declared
Season time;
and it can be assigned a value
time = Season.fall;
6/30/2016
CS110-Spring 2005, Lecture 21
15
Enumerated Types
The values are specified through the
name of the type
Enumerated types are type-safe – you
cannot assign any value other than
those listed
6/30/2016
CS110-Spring 2005, Lecture 21
16
Ordinal Values
Internally, each value of an enumerated
type is stored as an integer, called its
ordinal value
The first value in an enumerated type
has an ordinal value of zero, the second
one, and so on
However, you cannot assign a numeric
value to an enumerated type, even if it
corresponds to a valid ordinal value
6/30/2016
CS110-Spring 2005, Lecture 21
17
Enumerated Types
The declaration of an enumerated type
is a special type of class, and each
variable of that type is an object
The ordinal method returns the
ordinal value of the object
The name method returns the name of
the identifier corresponding to the
object's value
6/30/2016
CS110-Spring 2005, Lecture 21
18
Iterators
An iterator is an object that allows you
to process a collection of items one at a
time
It lets you step through each item in
turn and process it as needed
An iterator object has a hasNext
method that returns true if there is at
least one more item to process.
6/30/2016
CS110-Spring 2005, Lecture 21
19
Iterators
The next method returns the next item
Several classes in the Java standard class
library are iterators
The Scanner class is an iterator
the hasNext method returns true if there is more
data to be scanned
the next method returns the next scanned token
as a string
6/30/2016
CS110-Spring 2005, Lecture 21
20
Iterators
The Scanner class also has variations
on the hasNext method for specific
data types (such as hasNextInt)
The fact that a Scanner is an iterator
is particularly helpful when reading
input from a file
6/30/2016
CS110-Spring 2005, Lecture 21
21
Iterators
Scanner object can be set up to read
each line of the input until the end of
the file is encountered
6/30/2016
CS110-Spring 2005, Lecture 21
22
Iterators and for Loops
A variant of the for loop simplifies the
repetitive processing the items
For example, if BookList is an iterator
that manages Book objects, the
following loop will print each book:
for (Book myBook : BookList)
System.out.println (myBook);
6/30/2016
CS110-Spring 2005, Lecture 21
23
Iterators and for Loops
This style of for loop can be read "for
each Book in BookList, …"
Therefore the iterator version of the
for loop is sometimes referred to as
the foreach loop
It eliminates the need to call the
hasNext and next methods explicitly
6/30/2016
CS110-Spring 2005, Lecture 21
24