What should be done in the case of a Java invalid input exception?
Should you throw an exception if a method's input values are out of range? eg
//no imaginary numbers
public int MySquareRoot(int x)
{
if (x<0> {
throw new ArgumentOutOfBoundsException("Must be a non-negative integer");
}
//our implementation here
}
Now this method should never be called with a non-negative number, but hey programmers make mistakes. Is throwing exceptions here the right thing to do?
Regarding the java invalid input exception -
Yes, I believe you should throw an exception, to help your fellow programmers notice the error at compile-time, by also throwing ArgumentOutOfBoundsException in your method declaration.
That is, unless you use imaginary numbers in your project, where -1 is a perfectly valid argument for your square root method.