For my classes that have methods with similar input-checking logic (for example, a custom multi-dimensional array that has a lot of methods, all of which check if given coordinates are within the array limits), I create a separate private checker that throws runtime exceptions, and also a public checker, that just returns a boolean value indicating if a variable is acceptable for this class methods. Here's example:
public class Foo {
public void doStuff(Variable v) {
checkVariableUnsafe(v);
... // do stuff
}
private void checkVariableUnsafe(Variable v) throws InvalidVariableException {...}
public boolean checkVariable(Variable v) {
try {
checkVariableUnsafe(v);
return true;
} catch (InvalidVariableException e) {
return false;
}
}
}
Is it OK to use it, or are there some downfalls that I fail to see? What's the commonly used pattern in such situations?