posted 19 years ago
The finally block is guaranteed to execute regardless of what happens in the "try{ }" and "catch{ }" blocks. The only exception is if the application shuts down, either with an unrecoverable error (Java out-of-memory exception) or an explicit "System.exit()" call.
Try the following code:
The output in both cases is "3", meaning that the "finally" block was executed last and it's return was honored. This will give you a warning message when you compile it, but it does compile and run. The first call to "ugly" throws an exception (Integer divide by zero) and ends up in the "catch" block. The second call to "ugly" works correctly. But both "deferred" to the "finally" block's return.
Needless to say, this is *ugly* programming of the highest order and should be avoided at all cost.
As a general note, you can always figure out what Java actually does by writing a simple program and seeing what the results are ;-)
[ October 29, 2003: Message edited by: Wayne L Johnson ]