What to return upon failure of a generic function
I'm making myself a simple stack container (it's an array with push/pop). I've got it working with int, but now am trying to bring it over to being generic. My code:
I've faltered at line 3. What to return? Should I throw an exception? Doe Java already have an exception handle for this?
public E pop()
if (position == 0) return 0;
Re: What to return upon failure of a generic function
There's IllegalStateException. And java.util.Stack throws an EmptyStackException.
Both extend RuntimeException for the implications of which see the Exceptions chapter of Oracle's Tutorial.