Solved

How to exit from the code

Posted on 2002-03-13
7
214 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 56
map related example 6 49
how to add new optional parameter to JSP 1 42
How to convert String matching to regex in java 4 34
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

821 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