Compile and run this sample code and press Crt+C to exit the application, you can see that "System is Shutting Down" message prints before application exiting.

But we have to be careful when using this, because if the shutdown code we attach to the shutdown hook end up in an infinite loop, our application will be stuck and only way to exit will be killing the process.