The Department of Computer Science & Engineering |
STUART C. SHAPIRO: CSE
116 B
|
java.lang.Throwable
.
try
statement:try { Statements during which one or more exceptions might be thrown. } catch (ExceptionType param) { [optional] Code to handle exception. } catch (ExceptionType param) { [optional] Code to handle exception. } catch (ExceptionType param) { [optional] Code to handle exception. } finally { [optional] Statements to be executed: after the try statements if no exception was thrown; after the code that caught an exception; after the try statements if an exception was thrown and not caught } Statements to be executed after the try statements when no exception is thrown, or after the catch code when an exception has been thrown. (Finally statements done first.)
throws
Clause
IOException
.
All "checked" exceptions must be planned for.
If a method might
throw a checked exception, the code that calls that method must be in
a try
statement, or the method in which that code appears must
be declared with a throws
clause.
Object.clone()
Now it can be told.
clone
method of the Cloneable
interface throws
the
CloneNotSupportedException
, which is checked, so if you
implement and use clone
, you need to use try
and/or throws
.
Exception
(for checked exceptions), or RuntimeException
(for unchecked exceptions).
For different versions of the echo loop, see Echo2.java, and Echo3.java.