Results 1 to 5 of 5

Thread: System.exit

  1. #1
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    System.exit

    what is the difference between System.exit(0) and System.exit(1) ?

    ----------------
    suresh


  2. #2
    Junior Member
    Join Date
    Jul 2007
    Answers
    2

    Smile Re: System.exit

    zero usually means normal termination,
    while other return values usually means an error condition


  3. #3
    Junior Member
    Join Date
    Jul 2007
    Answers
    4

    Re: System.exit

    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)


  4. #4
    Junior Member
    Join Date
    Jul 2007
    Answers
    12

    Re: System.exit

    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.


  5. #5
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: System.exit

    Thanks for all your reply....

    ----------------
    suresh


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact