2

I am using below script(.sh file) to run a java code in an UNIX system,but even if Java is giving exception or terminated successfully in both cases, exit code coming as 0, I want to return non zero exit codes from script, if Java run throwing an exception, so that I can add if-then check in script,to print success or failure messages.

#!/bin/sh
echo 'processing started -->>'
LOC=/opt/appl/Mapping/

export JAVA_HOME=/usr/java6
export PATH=/usr/java6/bin

LD_LIBRARY_PATH=/opt/appl/JARS/

export LD_LIBRARY_PATH

java -classpath  $LOC/ojdbc6-11.2.0.3.0.jar:$LOC/ds35-02.00.11.jar:$LOC/log4j-1.2.17.jar:$LOC/TestClasses.jar:$LOC/db2jcc_license_cisuz-3.0.0.jar:$LOC/db2jcc_license_cu-3.0.0.jar:$LOC/db2jcc-3.0.0.jar -Xms256M -Xmx512M com.home.backfill.TestRun

I can use Try-catch in Java file and use System.exit(1) at catch block, but I am looking for any good generic approach, as My code could be very long, not sure if it would be good idea to put System.exit(1) in every catch block.

6
  • Just as a thought can you try aspectJ or which is on concept of AOP you have to write advice for exceptions and can System.exit() Commented Apr 11, 2016 at 8:50
  • You can of course do System.exit(1) in the try-catch within the main method only. In all other places, you either remove the catch-blocks (if they would otherwise do System.exit(1), or re-throw the exception from the catch-block, so that it's finally caught in main method. Commented Apr 11, 2016 at 8:53
  • You could instead, call a singleton class (e.g. .fatalError()) in every catch block that calls System.exit(1). That way you'll have more maintainability. Commented Apr 11, 2016 at 8:54
  • i usually put some System.exit(...) with different values depending on the type of exception thrown and the method that throws it. Each code is associated with a unique error label. For example: 1 for bad config file, 2 for some persistence error, etc. Commented Apr 11, 2016 at 8:56
  • @AlexShesterov I guess it would be best, I can create a generic exception class, and throw them from my every catch block, so that eventually it will be caught at my main method, and use system exit once. Thanks much for the idea. Commented Apr 11, 2016 at 8:59

2 Answers 2

1

If you want something generic:

interface ExceptionAction { 
    public void apply() throws Exception;
}

class Catcher {

    public static void invoke(ExceptionAction ea) {
        try{
            ea.apply();
        } catch(Exception e) {
            System.exit(1);
        }
    }

}

Then invoke by creating lambdas. (You will have to set any 'return' value to a captured local variable):

Catcher.invoke(() -> {
    /// your code here
});

I would note that this is about as verbose as try/catch though, and the latter is probably more readable/colloquial.

Sign up to request clarification or add additional context in comments.

Comments

0

If I understood you correctly, why don't you ran a generic function that determines what kind of code to return?

try {
//your code here
} catch(YourException e){
   Handler.handle(e);
}

Where Handler.handle(e) takes an exception and determines the Integer with which to shutdown the system. At least there you have a centralized point of where the system will shutdown.

2 Comments

Thanks for your suggestion it would also work, but for now, I am going with Alex's die, a generic exception class, because I would only require to show if the program failed or passed, but your idea would give much more freedom, I can use different system exit code for different exception.
Imagining what you are trying to do, and believing that the program won't be an entire application, I would also think that his advice is very good and worth following! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.