Think about what you expect that method to return:
1) an arbitrary integer or
2) a failure status
If you can sacrifice one int value to indicate a failure status you can map all possible return values to the int domain: e.g. x == Integer.MIN_VALUE indicates a failure status otherwise x is the int return value for the method.
If you can't find such a sentinel value think classes; make your method return an instance of this class:
-
class ReturnValue {
-
private int x;
-
private boolean failure;
-
//
-
ReturnValue() { failure= true; }
-
ReturnValue(int x) { this.x= x; }
-
//
-
boolean isFailure() { return failure; }
-
int getX() { return x; }
-
}
-
Your method will look something like this:
-
ReturnValue yourMethod( ... )
-
try {
-
return new ReturnValue(Integer.parseInt( ... ));
-
}
-
catch (Exception e) {
-
return new ReturnValue();
-
}
-
}
-
... and the callers of this method can check the 'validity' of the returned value and, if no error occurred, get the int value from the ReturnValue object. Classes and objects aren't part of the language for no reason.
kind regards,
Jos