Lesson 7 | The try-catch Statement |
Objective | Describe how Exceptions are caught |
try
-catch
statement. The try
part of the statement surrounds the statements for
which exceptions are to be caught, and the catch
part identifies the exception class that is caught and how the exception is to be processed.
Multiple catch
clauses may be used. An optional finally
clause identifies processing that is to be performed whether or not an exception is thrown or caught.
Only one finally
clause may be used. catch
clause declares a variable that identifies the type of exceptions that it catches. The type of this variable may be any class that extends Throwable
.
The catch
clause catches any exceptions that are subclasses of this type.
If multiple catch
clauses are supplied, the first matching catch
clause is executed. Subsequent catch
clauses are ignored. finally
clause identifies code that is to be executed after the catch
clause (if any) or the try
statement.
try
-catch
statements may be nested within each other.
The nested try
-catch
statement is contained in the try
clause of the try
-catch
statement in which it is nested.
switch
statements, loop
execution, and Exception handling.