• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.exit();  RSS feed

 
shivang sarawagi
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone enlighten me about the arguments of the method System.exit(); How does the functionality of the method vary when we provide argument as 0,1 and -1
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is customary to use 0 for normal exit and non-zero for exit because of some sort of error. Some environments (eg NetBeans) will show "exited with value of 123", but the exit value is often not used. It makes no difference to the functionality. I think it might be a throwback to the days of C, where error codes can be passed back to the operating system from an application from the main function.

Beware of System.exit() in a threaded environment (which includes Swing); if you have something else going on, eg writing to a file, in another thread, System.exit() will terminate that thread too, and you will have a partially-written file with corrupted data.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exit code is still used in shell scripts, to check whether or not a program has exited successfully or with some kind of error. These exit codes are used to chain applications together*, or to run some code conditionally depending on the exit code. As Campbell said, the convention is that 0 means success and everything else means failure.


*For instance, the well known Linux build command ./configure && make && sudo make install. The && works like the Java &&; if ./configure exits[/strike]ts successfully (with exit code 0), make is executed. If ./configure produces an error, make and sudo make install are skipped.

[Edit]Small typo[/Edit]
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Rob. I didn't know that.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren't you using Linux as your main operating system?
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but I've only ever tried
configure
make
make install
clean

without the &&
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then it makes sense.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!