Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to exit from the code

Posted on 2002-03-13
7
Medium Priority
?
245 Views
Last Modified: 2010-03-31
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
Comment
Question by:Amulya
7 Comments
 
LVL 4

Expert Comment

by:kylar
ID: 6862645
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
 
LVL 1

Expert Comment

by:viswa081400
ID: 6862723
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
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6863142
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:kotan
ID: 6863446
or you can throw an exception,

if (!fil.exists())
{
   throws new FileNotFoundException();
}
0
 

Expert Comment

by:pritam_dewan
ID: 6864203
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
 
LVL 4

Expert Comment

by:kylar
ID: 6865202
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
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8289915
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question