4

I want to catch an exception (or something else) when my program is killed by a kill command from the operating system.

I want to print a message to a log file before closing.

Is there a way to do it?

Thanks

summerbulb
  • 5,709
  • 8
  • 37
  • 83
zeevblu
  • 1,059
  • 2
  • 11
  • 26
  • 1
    I have implemented working example in answer to [this](http://stackoverflow.com/questions/9277630/windows-shutdown-hook-on-java-application-run-from-a-bat-script/9277766#9277766) question. Unfortunately, it uses JNI, that not always acceptable. – lxbndr Feb 29 '12 at 12:55

1 Answers1

6

Try adding a shutdown hook

Richard Walton
  • 4,789
  • 3
  • 38
  • 49
  • you can not catch SIGKILL signal as opposed to SIGTERM. – bhups Feb 29 '12 at 12:23
  • This will work in most cases. This will *not* work when killing process with `kill -9` on Unix and may fail when `OutOfMemeoryException` was thrown. – AlexR Feb 29 '12 at 12:25