What happens if we use System exit(0) in try block Does the finally block gets executed or not.
Finally Block will not be executed if there is System.exit(0) in try block. The java.lang.System.exit() method exits current program by terminating running Java virtual machine (JVM). This method takes a status code. A non-zero value of status code is generally used to indicate abnormal termination. This is similar exit in C/C++. Following is the declaration for java.lang.System.exit() method: public static void exit(int status) exit(0) : Generally used to indicate successful termination. exit(1) or exit(-1) or any other non-zero value – Generally indicates unsuccessful termination. Note : This method does not return any value.
Finally Block will not be executed if there is System.exit(0) in try block. The java.lang.System.exit() method exits current program by terminating running Java virtual machine (JVM). This method takes a status code. A non-zero value of status code is generally used to indicate abnormal termination. This is similar exit in C/C++. Following is the declaration for java.lang.System.exit() method: public static void exit(int status) exit(0) : Generally used to indicate successful termination. exit(1) or exit(-1) or any other non-zero value – Generally indicates unsuccessful termination. Note : This method does not return any value.