tags:

views:

62

answers:

4

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

Is this a valid approach?

Here is 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());
    }
}
+1  A: 

Yes, nothing wrong with it. personally I wouldn't bother with an Enum for it, a few consts would suffice.

Omry
A: 

This looks fine. It's especially important that your success code is 0, since that's the convention and scripted environments will specifically check for this and assume a non-zero value is an error.

Brian Agnew
+3  A: 

The convention is to have
- 0 for success
- Positive numbers for warnings
- Negative numbers for errors

The history behind it is that error codes in some environments need to be an 8-bit value. For severe errors by convention the high bit is set (which effectively makes it -127 -> -1 for errors and 1 -> 127 for warnings)

Of course ANSI-C 99 defines only two macros, EXIT_SUCCESS, which is 0 and EXIT_FAILURE which is some other number that is undefined in the spec.

Romain Hippeau
I've never heard of that. Is is a Windows convention or something?
Stephen C
@Stephen - I updated my answer with some more info.
Romain Hippeau
@Romain - "in some environments" ... please be more specific. I have been developing and using software on various flavours of UNIX/Linux for 20+ years and I've never heard of a convention that positive return codes are just warnings. Please cite a reference.
Stephen C
A: 

Looks OK. You could put the OK statement in a finally block. I like the use of enums for fixed sets of constants.

Blair