Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-23-2008, 11:47 PM
Member
 
Join Date: Feb 2008
Posts: 45
new_2_java is on a distinguished road
System.exit() in catch block.
Hi all,

I have a program, which is invoked from a shell script. I need to return back to the shell script a status, whether the program succeeded or failed, so the shell script determines whether to continue with its normal course of actions or to stop upon faileur.

So, here's what I am assuming, and please do correct me if I am wrong.

I am planning to catch all the possible exceptions and upon catching each one, and properly handeling, I do a System.exit(2). Besides, at the very end of program flow, I do a System.exit(0).

So, in the shell script, I will check the value of $? by echoing it, and if it is 0, then the program ran successfully, else, there was some problems.

i.e.
Code:
public class HelloWorld { public static void main( String[] args ) { try { callFirstMethod(); callSecondMethod(); System.exit(0); } catch (Exception e) { e.printStackTrace(); System.exit(2); } }
}

is my theory correct? please comment.


Thanks.

Last edited by new_2_java : 06-23-2008 at 11:52 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-24-2008, 02:27 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 880
Nicholas Jordan is on a distinguished road
Probably.
Docs do not make a definite that the exit code is returned to system.

Runtime (Java 2 Platform SE v1.4.2))

All it says is
Quote:
public void exit(int status)Terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
Run some trials.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-24-2008, 06:39 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, exit code is not returned to the system back. You have think another way to do it. Most possible thing in that check for the success or failed of the exit.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-24-2008, 07:39 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
You should do the tests. but all that shows is that with your system, OS, JVM, etc. on that day, you get the expected value.

It is considered bad form to put a System.exit() call in your code, it will not work properly if someone reuses your code in Tomcat or a J2EE system.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-24-2008, 08:15 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
As much as possible that way in my codes to exit the application. Most of the time return a value and exit the specific method or from the whole application.

As fishtoprecords says, there is a reason for me too. I don't know about Tomcat, but in J2EE this is killer. I have an experience on it on very first commercial project in J2EE. I have to work for two days continuously to fix it.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-24-2008, 08:51 AM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 356
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Oh man if you run the chain of commands in shell script ,so why don't you "ask" shell if the exit was 0 or 1 in shell script itself?It is easier way to do,then you can transfer the exit output of shell to Java application and then to know whether continue or not.
to read the shell output in java i posted at some topic here in forum

Last edited by serjant : 06-24-2008 at 08:57 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-24-2008, 09:02 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
Do folks write filters in java to use in shell scripts? I've never seen that done in the ten years that I have been writing java. I use perl for filters.

Obviously you can write anything in any language, but I'd never think to use Java for that.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-24-2008, 09:02 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Is there a way. I don't know about such. Thanks for the information.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-24-2008, 05:45 PM
Member
 
Join Date: Feb 2008
Posts: 45
new_2_java is on a distinguished road
Thank you guys for your inputs/suggestions/comments.

>>Oh man if you run the chain of commands in shell ....

That's exactly what I am trying to do. Read the original post again.

A little more background:

The shell script(I call it driver here), which is a driver wrapper arround a handful of other scripts which is in variouse formats java/plsql/other shell scripts. So, the java program is invoked by this driver script. i.e.
Code:
java -cp "$MYCLASSPATH" HelloWorld if [ $? = 0 ] ; then echo "Program ran successfully" else echo "Program encountered a problem" fi
I have done a couple of tests and it seems to return the correct status 0 or 2.

Thanks once again all.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
system.exit(..) ramakanta.majhi New To Java 2 06-14-2008 03:28 AM
Try Catch block issues kewlgeye New To Java 11 04-29-2008 09:10 AM
try...catch block javaplus New To Java 3 11-06-2007 09:53 PM
help with System.exit (1) function call katie Advanced Java 2 08-06-2007 10:03 PM
How to exit the program.. coco New To Java 1 08-01-2007 07:56 PM


All times are GMT +3. The time now is 12:41 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org