I'm running my Java application in cmd.exe
in Windows. If I stop the process forcefully by pressing Ctrl-C, and the code at that moment was running in the try
block, will the finally
block still be executed?
In my tests it seems that, yes, it is executed.
While your finally code may have executed on your Windows machine (I couldn't reproduce it with Linux), according to this documentation on finally:
So I wouldn't use a finally block to make sure a piece of code executes, even if the user tries to prematurely exit. If you need that, you can use, like Adrian Petrescu mentioned, Shutdown Hooks