Solved

How to exit from the code

Posted on 2002-03-13
7
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 630
ejb example issues 3 91
jboss 7.1 start up error 1 85
Retrieve multiple results between delimiters in a String in Java 13 29
In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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 …
Suggested Courses

710 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