• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt about System.exit() method  RSS feed

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Please tell me what is the use of

System.exit(n) statement and the possible real time scenarios to include it in our code?

Also when should we use Syste.exit(0) and when to use System.exit(1)?

Whats the difference between the two?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What part of the documentation did you not understand?

As for "real-time scenarios," I assume you mean "real-world," not "real-time." And we don't usually use it at all. With proper program control structures, we don't usually need it.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation says the following about the exit method of System class


* Terminates the currently running Java Virtual Machine. The
* argument serves as a status code; by convention, a nonzero status
* code indicates abnormal termination.

What is meant by "abnormal termination"..

When do we use non zero argument for the exit method
and when do we use the zero argument?

Whats the difference between the two?
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parameter to System.exit() is passed to the caller of the Java program, which is generally a batch script of some kind. If it's zero, then the script should assume the application terminated normally. If not, then the script should assume there was some kind of problem.

Of course, then, "normally" would mean whatever the writer of the program chooses it to mean. The writer of the program might also choose to return a variety of non-zero values, to indicate a variety of error conditions.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don’t use a batch script or similar, you don’t see the return value from exit().
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you don’t use a batch script or similar, you don’t see the return value from exit().


I don't know how Windows works in this regard, but in Linux, all the bourne family shells (and, I think, the C family shells) put the value of the last command executed will be in the ? variable, so it's available when executing in a terminal window. So, for example, if you do System.exit(123) in your java code, then you can do this:



 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never knew that. Thank you
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:
Campbell Ritchie wrote:If you don’t use a batch script or similar, you don’t see the return value from exit().


I don't know how Windows works in this regard, but in Linux, all the bourne family shells (and, I think, the C family shells) put the value of the last command executed will be in the ? variable, so it's available when executing in a terminal window. So, for example, if you do System.exit(123) in your java code, then you can do this:





Campbell Ritchie wrote:Never knew that. Thank you


One way to think about it is .. the difference between running in command line and a batch file is the source of the commands. So, anything that you can do in a batch, you should be able to do from the command line.

Henry
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:What is meant by "abnormal termination"..

When do we use non zero argument for the exit method
and when do we use the zero argument?

Whats the difference between the two?

you probably won't like this answer, but "abnormal termination" means whatever you want it to mean. Here is a real example.

I wrote a perl script that is supposed to connect to a database, run a query, parse the results, write something to a log file, and send an email to someone that says "here are the results". I wrote is such that:

  • If my program did everything it should (i.e. exited normally), I would have it exit with a "0".
  • If it couldn't connect to the database, I would exit with a "1"
  • If it couldn't run the query, I would exit with a "2".
  • If there were no results to process (and there always should have been), exit with a 3
  • If the log file couldn't be opened or written to, exit with a 4
  • If it couldn't send the email, exit with a 5


  • Note that these values mean nothing to anyone but me - or the other members of my team. This script was called as a windows scheduled task (similar to a cron job). The windows task scheduler logs the return code of the program it calls. So, I could go through THAT log file and look for any "abnormal termination code" - i.e. anything that is not a "0".
     
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't know how Windows works in this regard, but in Linux, all the bourne family shells (and, I think, the C family shells) put the value of the last command executed will be in the ? variable

    Windows always used to put the return code of the last command executed in an environment variable called ERRORLEVEL. I would imagine this behaviour hasn't changed although I haven't tried it since Win XP.

    You can output the value from a command prompt as follows:
     
    Greenhorn
    Posts: 11
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tony Docherty wrote:
    I don't know how Windows works in this regard, but in Linux, all the bourne family shells (and, I think, the C family shells) put the value of the last command executed will be in the ? variable

    Windows always used to put the return code of the last command executed in an environment variable called ERRORLEVEL. I would imagine this behaviour hasn't changed although I haven't tried it since Win XP.

    You can output the value from a command prompt as follows:

    I've just tried this in Windows 7 and it still works.
     
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ruud Smith wrote:I've just tried this in Windows 7 and it still works.

    And yet the whole business of return codes is platform-dependent; which is NOT what Java is about. But I think Fred already said that.

    My advice: DON'T rely on them. I don't even use them.

    Winston
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    And yet the whole business of return codes is platform-dependent; which is NOT what Java is about. But I think Fred already said that.

    My advice: DON'T rely on them. I don't even use them.


    They're more command- or application-dependent than platform-dependent. Although on Linux the convention is for 0 to mean success and non-zero to mean failure of some sort, what counts as "failure" and which error code different kinds of failures produce varies by which command you're executing.

    In that vein, we can use System.exit() to provide a platform-independent but our-application-dependent means of communicating the final status to the invoker of the JVM. Whether it's $? on Linux or %ERRORLEVEL% on Windows or some other approach on some other system, we can define and document our exit codes and know that a conforming JVM will make them available to whatever invoked it.

    Having said all that, I still wouldn't use System.exit() unless I was writing a command-line utility and expected it to be used as part of a shell script.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!