Java Application Return Codes

I have a Java program that processes one file at a time. This Java program is called from a shell script that registers a return code from a Java program. There are 2 types of errors. Expected errors and unexpected errors. In both cases, I just need to register them. My wrapper knows about three different states. 0-OK, 1-PROCESSING_FAILED, 2- ERROR.

Is this a valid approach?

Here's my approach:

enum ReturnCodes {OK,PROCESSING_FAILED,ERROR};

    public static void main(String[] args)
    {
        ...
            proc.processMyFile();
        ...
        System.exit(ReturnCodes.OK.ordinal());
    }
    catch (Throwable t)
    {
        ...
        System.exit(ReturnCodes.ERROR.ordinal());
    }


private void processMyFile()
{
    try
    {
        ...
    }catch( ExpectedException e)
    {
        ...
        System.exit(ReturnCodes.PROCESSING_FAILED.ordinal());
    }
}

      

+2


a source to share


4 answers


The agreement is to have

  • Zero for success
  • Positive numbers for warnings
  • Negative error numbers


The story behind it is that error codes in some environments must be 8-bit. For serious errors, the most significant bit is set by convention (which actually makes it -127 -> -1 for errors and 1 -> 127 for warnings)

Of course, ANSI-C 99 only defines two macros, EXIT_SUCCESS

which is 0 and EXIT_FAILURE

which is some other undefined number in the specification.

+4


a source


It's okay. personally I wouldn't bother with Enum for this, a few consts would be enough.



+1


a source


It looks great. It is especially important that your success code is 0, as convention and scripting environments will specifically check for this and assume that a non-zero value is an error.

0


a source


Looks nice. You can put an OK statement in a finally block. I like using enums for fixed sets of constants.

0


a source







All Articles