GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes system.exit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "system.exit" Watch "system.exit" New topic
Author

system.exit

Adrienne Peck
Greenhorn

Joined: Jul 10, 2006
Posts: 29
i have to write code that uses 'System.exit(*)'. all i know of is 'System.exit(0)'. what does this mean? where can i find information on other parameters to send besides zero?
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
there is no documentation about the values you can send. it's up to you.
if you want to you can use System.exit(12345) to terminate your program in OK cases and System.exit(0) in NOT OK cases.... it's up to you.

pascal
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The parameter becomes the "exit status" of the process -- it's sent back to the operating system as-is, and Java doesn't even look at it.

By convention, 0 means "normal termination" and anything else means "error", but not all programs conform to that. It's 100% up to the individual program to choose different exit values and assign meaning to them. The returned value is most often simply ignored.

In practice, you'll rarely see anything but System.exit(0); when you do, it's generally exit(-1) to indicate an error.


[Jess in Action][AskingGoodQuestions]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

The parameter you use there will (usually) be passed back to whatever called your Java program under the name of something like "error code". And that "whatever", which is often a script or shell program, can do something based on the value.

Traditionally zero means "Terminated normally". Non-zero values are up to you to specify.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
I see a few people "snuck in" with answers while I was writing mine. I'll add some shell/bat scripting to show how it might work.
----

Well it all depends on how the return value of the executable is to be used. On Unix systems, for instance, a return value of 0 means that the program was successful. In sh, bash, etc., an exit value of 0 is "true" (contrary to the C language standard).

So something like...


would work as one might expect if LookForFile did an System.exit(0) if the file referenced by the command line argument exists and System.exit(1) if the file does NOT exist.

In Windows BAT files, the errorlevel command is used to check the exit value of the previous program and returns "true" if the exit value was greater than or equal to the numeric argument. The shell script above would translate to something like this in a Windows bat file.



Again the standard is to exit with a zero if everything was successful. The menaing of any other exit value depends on what the program does.
[ August 02, 2006: Message edited by: Ryan McGuire ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by Ryan McGuire:
I see a few people "snuck in" with answers while I was writing mine.
Yeah, this is today's edition of "Ranchers Idol". Simon Cowell will be around later to judge our contributions...

Adrienne Peck
Greenhorn

Joined: Jul 10, 2006
Posts: 29
So when applying the System.exit(-1) to a main() means that if there's an error, we should terminate.

I see that we can do what we want within an 'if' statement when -1 is returned. If -1 is returned without being specified in an 'if' statement, does the JVM terminate the program?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

No, that's not it at all -- you're making it vastly more complex than it really is.

As I said, the JVM does not interpret the argument to System.exit(). It doesn't matter what value you pass: the program will immediately terminate and return that value to the operating system. That's it.

The OS, or a script or program that launched your Java program, might then look at that value and decide to do something about it -- but at that time, your Java program is already gone.
Adrienne Peck
Greenhorn

Joined: Jul 10, 2006
Posts: 29
Ok, so then let's say that we want to throw an exception to the main(). The code for System.exit would be there. If an exception is thrown, then we would code 'System.exit(-1)'. If there is no exception thrown, we would code 'System.exit(0)'. So, then, I should really learn how to throw an exception so that System.exit(?) gets set to what ever value we determine. Is this correct?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yes, indeed, you could do that.



Note that if a program exits due to an uncaught exception in main(), Sun's JVMs do, actually, return "1" as an exit status; if it terminates normally, then "0" is returned. So in fact, the above isn't really necessary.
 
wood burning stoves
 
subject: system.exit