RSS Feed for This PostCurrent Article

Java: Understanding System.exit

Have a look at the following code

   1: public class Sample {
   2:  
   3:     public static void main(String[] args) {
   4:  
   5:         try {
   6:  
   7:             System.out.println("Processing...");
   8:  
   9:             System.exit(0);
  10:  
  11:         } finally {
  12:  
  13:             System.out.println("Clean up");
  14:  
  15:         }
  16:  
  17:     }
  18:  
  19: }

The finally block will never get executed if you think that it will always get executed.

The System.exit method halts the execution of the current thread and all others dead in their tracks. The presence of a finally clause does not give a thread special permission to continue executing.

For any clean up before JVM exits, you must use ShutdownHook.


Trackback URL


Sorry, comments for this entry are closed at this time.