Results 1 to 9 of 9
  1. #1
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default 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.
    Java 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 09:52 PM.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default 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
    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  6. #6
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    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 06:57 AM.

  7. #7
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    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.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  9. #9
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    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.
    Java 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.

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •