Java application return codes
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开发者_如何学Go 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());
}
}
The convention is to have
- Zero 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.
Yes, nothing wrong with it. personally I wouldn't bother with an Enum for it, a few consts would suffice.
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.
Looks OK. You could put the OK statement in a finally block. I like the use of enums for fixed sets of constants.
精彩评论