Does Java support shutdown code when program is killed?

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

I want my program to print something when I terminate it by hitting
control-C. Is this possible? How do you do this in Java?

Thanks!
 
S

Simon

Thomas said:
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)>


Not sure if System.out is guaranteed to be available at this point though.

I'm also not sure about whether or not availability of System.out is guaranteed
in any way, but I'm sure that it worked for me at least once. At least you can
be sure that it does not throw an exception since it is a PrintStream :)

The docs of addShutdownHook say:

"Uncaught exceptions are handled in shutdown hooks just as in any other thread,
by invoking the uncaughtException method of the thread's ThreadGroup object. The
default implementation of this method prints the exception's stack trace to
System.err and terminates the thread; it does not cause the virtual machine to
exit or halt."

So they also assume that System.err is available.

Cheers,
Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top