Use result codes (also called error codes) to indicate minor errors and issue warnings. A result code can be a simple Boolean value (typically, true indicates success, false indicates an error), or it can be a numeric value. The following code shows a setSize method that returns an integer as a result code.
// (Optional) Declare symbolic constants to represent code values.
public final int TOO_SMALL = -1;
public final int OK = 0;
public final int TOO_BIG = 1;
...
public int setSize(int newSize) {
if (newSize < 15) return TOO_SMALL;
if (newSize > 37) return TOO_BIG;
mSize = newSize;
return OK;
}
Code that calls setSize should test the return value to check for errors. For example,
public void grow(int amount) {
int result = setSize(amount);
String msg = "";
switch (result) {
case TOO_SMALL :
msg = "New size is too small.";
break;
case TOO_BIG:
msg = "New size is too big.";
break;
case OK :
msg = "New size is OK.";
break;
}
System.out.println(msg);
}