Thursday, 26 April 2018

Core Java: Terminating Program

System.exit(int exitCode): You could invoke the method System.exit(int exitCode) to terminate the program and return the control to the Java runtime. By convention, return code of zero indicates normal termination; while a non-zero exitCode indicates abnormal termination. For example,
 
if (errorCount > 10) {
   System.out.println("too many errors");
   System.exit(1);  // Terminate the program
}
 
The return statement: You could also use a "return returnValue" statement in the main() method to terminate the program and return control back to the Java Runtime. For example,
 
public static void main(String[] args) {
   ...
   if (errorCount > 10) {
      System.out.println("too many errors");
      return;  // Terminate and return control to Java Runtime from main()
   }
   ...
}

No comments:

Post a Comment

Please write your view and suggestion....