I'll explain what I mean by input error checking.
Say you have a function doSomething(x)
.
If the function completes successfully doSomething
does something and returns nothing. However, if there are errors I'd like to be notified. That is what I mean by error checking.
I'm looking for, in general, the best way to check for errors. I've thought of the following solutions, each with a potential problem.
Flag error checking. If
doSomething(x)
completes successfully returnnull
. Otherwise, it returns a boolean or an error string. Problem: Side effects.Throwing an exception. Throw an exception if
doSomething(x)
encounters an error. Problem: If you are performing error checking for parameters only, throwing anIllegalArgumentException
seems inappropriate.Validating input prior to function call. If the error checking is only meant for the parameters of the function, then you can call a validator function before calling the
doSomething(x)
function. Problem: What if a client of the class forgets to call the validator function before callingdoSomething(x)
?
I often encounter this problem and any help or a point in the right direction would be much appreciated.