java - Is it OK to use this pattern for value-checking? -
for classes have methods similar input-checking logic (for example, custom multi-dimensional array has lot of methods, of check if given coordinates within array limits), create separate private checker throws runtime exceptions, , public checker, returns boolean value indicating if variable acceptable class methods. here's example:
public class foo { public void dostuff(variable v) { checkvariableunsafe(v); ... // stuff } private void checkvariableunsafe(variable v) throws invalidvariableexception {...} public boolean checkvariable(variable v) { try { checkvariableunsafe(v); return true; } catch (invalidvariableexception e) { return false; } } } is ok use it, or there downfalls fail see? what's commonly used pattern in such situations?
it recommended avoid using exceptions normal program flow. without debating that issue here, if wanted follow advice, put logic check in public checkvariable method, , have private checkvariableunsafe method call checkvariable , throw exception if returns false.
i don't think there enough context in question comment definitively on appropriateness of api have created, can see nothing intrinsically wrong it.
Comments
Post a Comment