![]() |
|
Exception Handling in Java, Free online Core Java Programming Tutorial, Learn Java Programming Language online for free... |
| Lessons | Exception Handling in Java 4 |
|
|
There is something more to understand about the finally clause in order to use it effectively. If the finally clause includes a transfer of control statement (return, break, continue, throw) then that statement overrides any transfer of control initiated in the try or in a catch clause. First, let's assume that the finally clause does not include any transfer of control. Here are the situations that can arise:
If the finally block does include a transfer of control, then that takes precedence over any transfer of control executed in the try or in an executed catch clause. So for all of the cases listed above, the finally clause would execute, then its transfer of control would take place. Here's one example: try {
return 0;
}
finally {
return 2;
}
The result of executing this code is that 2 is returned. Note that this is rather confusing! The moral is that you probably do not want to include transfer-of-control statements in both the try
statements and the finally clause, or in both a catch clause and the finally clause.
Next >>> Lesson No. 21: Java Threads
|
|