Approach. 1 Using ShutdownHook
What is the shutdown hook?
Shutdown Hooks are a special construct that allows developers to plug in a piece of code to be executed when the JVM is shutting down.
Approach. 2 By overriding the SecurityManager
We can achieve it by overriding the checkExit method of SecurityManager class.
Now uncomment the line 10 i.e throw new SecurityException(). The new output will be: