I have a function like
public object getObj(int index,object[] a)
{
if(index>a.size)
//what to return??
else
return a[index-1];
}
What should I return if the index is bigger than size? I don't want to return null since null can be an object and I want to be able to distinguish between the cases that index is bigger than size and the object is null itself. How should I use java error handler classes?
When someone supplies a negative index or an index that is out of bounds, it is definitely a programming error. Programming errors are signaled in Java by throwing unchecked exceptions (i.e. ones deriving from RuntimeException
). In this specific case, you should throw IndexOutOfBoundsException
.