CIS 121 February 14, 2000
RECALL: Exam 1 will be this Thursday on Modules 1-3.
Note: The School of Computer and Information Sciences offers each
of its
majors the opportunity to obtain an account on our UNIX server to use for
e-mail and to host personal web pages. In order to activate your account,
go to http://www.cis.usouthal.edu/policy.html.
Read this policy carefully, and if you agree to abide by the terms, your
e-mail account and web space will be activated. Students in CIS 120 and
121 are encouraged to take advantage of this offer since when we begin
talking about applets, you will have a place in which to test them and
make them available. If you are interested in having your account and web
space activated, print out the form, sign it, and place it in my mailbox
or give it to me.
Another advantage to having an account on the UNIX server is that you may
use it to write and test Java programs. Unfortunately the machines in this
lab are slow when using JBuilder. On the UNIX system, you will not have
the advantage of a GUI. But the compilation and execution will be
relatively quick. You can access your account from this lab or from any
machine connected to the Internet. You may also log in multiple times so
that you can keep the file open for editing in one session and use the
other session for compilation and execution.
Recall also that the programming assignment is now Due at the beginning of
class on Wednesday.
Today we are going to look at Module 4. Module 4 deals with Exception
Handling.
An exception is an event that occurs during execution of a program
that makes continuing impossible or undesirable. For example, if we index
an array element that is out of bounds or divide by zero.
An exception handler is a piece of code that is run when an
exception occurs. Its purpose is to try to either resume execution or
terminate the program in a controlled manner.
Java implements a termination model. This means that Java handles
exceptions by stopping execution of a block of code if an exception or
error is thrown.
In Java, there is a superclass called Throwable. Throwable has two
immediate subclasses, Error and Exception. Error is a superclass that
deals with errors that we generally can't recover from, such as running
out of memory. Exception is a superclass with many subclasses of
exceptions from which we may be able to detect and recover from. Some of
these are IOException, and ArithmeticException.
Java was designed that users could make their programs robust. That is,
they should be able to handle any error that occurs and recover from it if
possible. This is why we use throws clauses in method headings. In the
throws clause we indicate what type of exception the code in the method
body might throw.
For any code that potentially might cause an exception we either have to
have a throws clause in the method header or we have to catch the
exception and deal with it ourselves.
In order for us to do this, Java offers the keywords try,
catch, and finally.
Suppose we are trying to execute a statement which might potentially cause
some kind of error, and we don't have a throws clause in our method
header. Then what we have to do is use the keyword try to try out the
code, and use the keyword catch to catch any exception that might be
thrown and deal with it.
The syntax of these keywords is as follows.
try {
some code that might cause exception
}
catch (SomeException1 exc) {
code to respond
}
catch (SomeException2 exc) {
code to respond
}
...
A useful thing that the catch block might produce is a listing of the
program stack. This will help us track down the error.
In order to do this we use the method exc.printStackTrace().
The keyword finally appears at the end of the catch blocks. If an
exception is caught, the code in the finally block will be executed after
the exception is dealt with. If an exception is not caught, finally will
be executed before the interpreter moves past the try block. finally is
optional but is used to clean up any memory we need to clean up before we
leave the try block. For example, if we open a file in the try block, we
can use finally to release it. The action of finally is similar to that of
the finalize() method in a class definition.
Today we are going to perform a type and run. If you turn to page 5 of
module 4, you will find the source code for a class called MyException. On
page 6 you will find the source code for a class called Except. Type both
of these in, but don't compile yet.