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.

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.

Sure, you can return any int you want from System.exit() and the convention is that a non-zero value is abnormal termination. You can return a specific int for any condition you wish to pass back to the script and report the specific problems or take further action based on that info.

I would recommend a highest-level catch for Throwable and reserve a specific int return code for that. If you hit that catch block, something blew up that all of your other handlers failed to address. You probably want to capture the stack trace and cause information for that Throwable and log it to an error log file.

Be sure to define named constants for any error codes you plan on returning and use those instead of the int values. You don't want to to have to go searching through code looking for '16' to figure out what happened with the code.

Sure, you can return any int you want from System.exit() and the convention is that a non-zero value is abnormal termination. You can return a specific int for any condition you wish to pass back to the script and report the specific problems or take further action based on that info.

I would recommend a highest-level catch for Throwable and reserve a specific int return code for that. If you hit that catch block, something blew up that all of your other handlers failed to address. You probably want to capture the stack trace and cause information for that Throwable and log it to an error log file.

Thansk for prompt reply. I agree with you totally, and that's what I am doing. the only reason, I wanted to confirm this, is that the shell script is a driver wrapper script arround a group of other scripts/programs ranging from perl/shell/sqlplus/java scripts, and the driver script needs to know whether any script succeeded or failed.

I do my error handling in my program and here's a portion of the code, where I handle the exception.

try {
     // do some stuff here
 } catch (DfException dfException) {
            dfException.printStackTrace();
            error.println(dfException.getMessage());
            System.exit(2);
        } catch (Exception e) {
            e.printStackTrace();
            error.println(e.getMessage());
            System.exit(2);
        } finally {
            try {
                colllection.close();
                
                // commit/roll back transaction
                if (transactionViable) {
                    sm.commitTransaction();
                } else {
                    sm.abortTransaction();
                }
            } catch (DfException e) {
                e.printStackTrace();
                error.println(e.getMessage());
                System.exit(2);
            }
        }

Yes, naming the exit value is a very good idea, i like that. But in my case, I only need to know, whether it failed or succeeded.

You only need to know failure or success now. And then later you'll need to know something else and after that something else will come up...
Just start with static final int constants for those return code from the start and even if you don't need to add more, reading this

System.exit(PROCESS_FAILURE);

is a lot clearer than

System.exit(2);

You only need to know failure or success now. And then later you'll need to know something else and after that something else will come up...
Just start with static final int constants for those return code from the start and even if you don't need to add more, reading this

System.exit(PROCESS_FAILURE);

is a lot clearer than

System.exit(2);

I totally agree. And I will do that. You are right, something like PROCESS_FAIL definitly makes sense than 2. :-)

Thanks once again, appreciate your inputs

I'm just finishing up my first semester of Java.

My last assignment is a "Battleship" program. I need to use System.exit(2) to abort the program and indicate an of: FileNotFoundException, ArrayOutOfBoundsException or arrays referring to the same point on a grid.

I understand that methods have parameters passed to them and return parameters to the method that called them.

I read, in the thread, that System.exit returns an int. I don't understand where it gets called from/where it's returning the int to.

Thanks for helping a neophyte!

This question has already been answered. Start a new discussion instead.