Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Doing a system exit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doing a system exit" Watch "Doing a system exit" New topic
Author

Doing a system exit

Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
I want to write an if statement such that if a condition is not met, I exit the system.
How do i do that?
My guess on how to do it is like this (it's wrong though):-
if (num < 0)
System.exit();
Any ideas, folks??

Cheers


John Bonham was stronger, but Keith Moon was faster.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Steve
what u guessed is right with one correction..
it shd be System.exit(<<some number>> ;
but people generally don't recommend this from amidst the program..instead have peaceful termination of program somehow...
if we were to exit() on the beginning of the main() given that the input arguments are not the way the program exects then it is fine but u should try to avoid exit() from a for loop or something in a method you know...the reason for this is, the application might be holding resources and they may get blocked as the application terminates...
regards
maulin.
Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
Originally posted by Maulin Vasavada:
hi Steve
what u guessed is right with one correction..
it shd be System.exit(<<some number>> ;
but people generally don't recommend this from amidst the program..instead have peaceful termination of program somehow...
if we were to exit() on the beginning of the main() given that the input arguments are not the way the program exects then it is fine but u should try to avoid exit() from a for loop or something in a method you know...the reason for this is, the application might be holding resources and they may get blocked as the application terminates...
regards
maulin.

Cheers!
Nice one
But what do the numbers such as (1) represent?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
well,
the numbers represent the 'return code'. in many languages like C/C++ etc we have "return codes" for the function and processes.
this number present return code for the process that our program is running in.
if u were a unix geek u sure knew this. all the time ppl write a shell script that checks a return code from the process to know if it were executed successfully or not and then depending upon that they make decision about what to do next in their script. so, at this time the return codes matters.
regards
maulin.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
If you were running your program from a batch file, the number in System.exit(0) might be useful. However, most of the time, unless you are creating an applet or a GUI application, you don't need to use System.exit(). The program will exit anyway.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
Don't get me started about those stupid light bulbs.
 
subject: Doing a system exit
 
Similar Threads
String Comparison ?
Enable/Disable Window Close X
how to check whether a file exits in a server or not ?
Uncaught exception
How to hide application instead of exiting