User is asked to select an ID from some given set. I check if that ID exist in my collection, if not I throw IndexOutOfBoundsException and catch it later. Can I actually use that exception for such purpose or is it just a very bad practice?
User is asked to select an ID from some given set. I check if
Share
throwing exceptions does really make sense if you facing really exceptional situation
if the contract of your class suppose than user will never query for a value not in collection, then exception is good. Perhaps IllegalArgumentException or something like this.
otherwise it leads to boilerplate code, and sometimes causes significant performance degradation because of stack unwinding and exception propagation
so it’s kind of trade-off whether to throw exception or return some pre-defined value
I’d suggest to return “null” in your case.