Results 1 to 2 of 2
- 09-16-2012, 04:34 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 21
- Rep Power
- 0
What to return upon failure of a generic function
Hi all,
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?Java Code:public E pop() { if (position == 0) return 0; return stack[--position]; }Last edited by Lowest0ne; 09-16-2012 at 04:37 AM.
- 09-16-2012, 05:16 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
Similar Threads
-
Creating generic wrapper of return type
By jackett_dad in forum Advanced JavaReplies: 1Last Post: 03-16-2012, 05:53 PM -
Return ArrayList function - Java
By skp123 in forum New To JavaReplies: 23Last Post: 05-01-2011, 04:11 AM -
Filling an array from the return value of the function
By alex1988 in forum Java AppletsReplies: 7Last Post: 02-02-2011, 09:29 AM -
Generic return type is not inferred in for-each loop
By ranma173 in forum Advanced JavaReplies: 5Last Post: 10-08-2010, 11:50 AM -
getSession() Function Return Null value
By dalchndr@gmail.com in forum Advanced JavaReplies: 0Last Post: 11-04-2009, 07:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks