• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt about System.exit() method

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27452
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never knew that. Thank you
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
    86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    I wasn't selected to go to mars. This tiny ad got in ahead of me:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic