Intermediate Programming Instructor: Greg Shaw

advertisement
Computer Programming II
COP 3337
Instructor: Greg Shaw
The Standard Java Exceptions
I.
The Throwable Class
A.
The class Throwable describes anything that can be thrown
as an exception.
B.
There are two types of Throwable objects
1. The Error class represents compile-time and system
errors that you usually don’t worry about catching.
2. The Exception class represents objects that can be
thrown from any of the standard Java library class
methods and from programmer-defined methods and runtime accidents.

The name of the exception thrown describes the
problem that occurred.
II. The RuntimeException Class
A.
RuntimeException is the base class for a group
exceptions that are thrown automatically by Java.
B.
RuntimeExceptions do not need to be included
exception specifications (i.e., “throw lists”).
C.
Because
they
indicate
bugs,
you
seldom
catch
a
RuntimeException – they are dealt with automatically -,
although you might still choose to throw them in your own
packages.
D.
If
a
RuntimeException
is
thrown
and
not
caught,
printStackTrace() will be called for that exception and
the program will terminate.
This is a big help in
debugging. (See program NeverCaught.java)
E.
The compiler
because
allows
you
to
ignore
in
of
your
RuntimeExceptions
1.
They represent programming errors you cannot catch. For
example, receiving a null pointer handed to your method
by a client programmer (which will generate a
NullPointerException), or
2.
They represent programming errors
checked
for
in
your
code,
ArrayIndexOutOfBoundsException.
you should
such
as
have
an
Download