Java Reference
In-Depth Information
< Day Day Up >
Puzzle 39: Hello, Goodbye
This program adds an unusual twist to the usual
Hello world
program. What does it print?
public class HelloGoodbye {
public static void main(String[] args) {
try {
System.out.println("Hello world");
System.exit(0);
} finally {
System.out.println("Goodbye world");
}
}
}
Solution 39: Hello, Goodbye
The program contains two
println
statements: one in a
try
block and the other in the
corresponding
finally
block. The
TRy
block executes its
println
and finishes execution
prematurely by calling
System.exit
. At this point, you might expect control to transfer to the
finally
block. If you tried the program, though, you found that it never can say goodbye: It prints
only
Hello world
. Doesn't this violate the principle explained in
Puzzle 36
?
It is true that a
finally
block is executed when a
try
block completes execution whether normally
or abruptly. In this program, however, the
try
block does not complete execution at all.
The
System.exit
method halts the execution of the current thread and all others dead in their
tracks.
The presence of a
finally
clause does not give a thread special permission to continue
executing.
When
System.exit
is called, the virtual machine performs two cleanup tasks before shutting down.
First, it executes all
shutdown hooks
that have been registered with
Runtime.addShutdownHook
.
This is useful to release resources external to the VM.
Use shutdown hooks for behavior that
must occur before the VM exits.
The following version of the program demonstrates this
Search WWH ::
Custom Search