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());
}
}
a source to share
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.
a source to share