Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

How to exit from the code

I would like to test if a file exists, if it does not then inform the user and exit.
 For this i did the foll:
  File fil = new File(file1);
  if(!fil.exist())
   {
    JOptionPane.showMessageDialog(this,"File1 does not exist");
  // How to exit from the code..should it be return
 //is there soemthing similar to exit as in shell scripting?
}
Thank you
0
Amulya
Asked:
Amulya
1 Solution
 
kylarCommented:
System.exit(int exitCode);

Generally exitCode = 0 where the program sucessfully exits or -1 (or other error code) when it terminates abnormally.

Cheers,
Kylar
0
 
viswa081400Commented:
As said by kylar, System.exit( int ) method is used to terminate the program. the integer passed is the return code that is returned to the script/app that initiated this program.


-Viswa
0
 
m_onkey_boyCommented:
If you want to just return from the method, but you don't want to kill your whole app, use:

if (fil.exists())
  return;
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
kotanCommented:
or you can throw an exception,

if (!fil.exists())
{
   throws new FileNotFoundException();
}
0
 
pritam_dewanCommented:
While system.exit(int) works fine i feel u shld create business exception and catch u from the calling program.this will help u to hv full control of ur code flow.
0
 
kylarCommented:
Pritam, generally speaking, it's considered bad to lock a question with an answer unless you are absolutely sure that your answer is exact. Otherwise, you should post a comment so that the question remains unlocked, since most experts won't look at questions that are locked. This also works since the asker can choose any comment made as an answer. Since your answer merely re-iterates what the other experts have commented, it would be considerate of you to change your answer to a comment.

Cheers,
Kylar
0
 
SpideyModCommented:
All,
I am unlocking this question in preparation for cleanup.  I will return in 7 days to finalize this question.  Please leave any recommendations for the final state of this question, I will take all recommendations into consideration.  Failing any feedback, I may decide in 7 days to delete or PAQ this question with no refund.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now