what is the difference between System.exit(0) and System.exit(1) ?
----------------
suresh
Printable View
what is the difference between System.exit(0) and System.exit(1) ?
----------------
suresh
zero usually means normal termination,
while other return values usually means an error condition
Hello,
basically, System.exit(int), you declare only an integer in class System and its method exit.
So,
These integer inside the method indicate whether the program that you write executes successfully , that is a successful compilation, and interprets.
you declare a '0' while your program executes successfully.
i.e., when your program has no error. and you declare this at the end of the code.
but, while your program as you think may occur with an error at that point or at that method end you declare System.exit(1).
then, if your program really has some error then it will show an error message at a point System.exit(1) .
observe the following code carefully to understand more.
package methods;
import java.util.Date;
public class Message2 {
public static void main(String []args) {
// Make sure the command
// line argument was passed in
if(null == args || args.length < 1) {
// Command line argument
// doesn't exist, so exit with
// an error flag
System.exit(1);
}
// Extract the only command line
// argument from the first element
// of the array
String message = args[0];
String formattedMessage = prependTimestamp(message);
System.out.println(formattedMessage);
}
public static String prependTimestamp(String str) {
Date now = new Date();
String timeStamp = now.toString();
return timeStamp + ": " + str;
}
}
System.exit(0)
both results in terminating our program execution. But below are the diffs:
System.exit(0): The User intentionally terminating the program.
System.exit(1): Some error caused for the termination.
For the readability purpose we use these. To differentiate the normal termination and termination due to error.
Thanks for all your reply....
----------------
suresh