Results 1 to 2 of 2
  1. #1
    Lowest0ne is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default 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:
    Java Code:
      public E pop()
      {
        if (position == 0) return 0;
        
        return stack[--position];
      }
    I've faltered at line 3. What to return? Should I throw an exception? Doe Java already have an exception handle for this?
    Last edited by Lowest0ne; 09-16-2012 at 04:37 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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

  1. Creating generic wrapper of return type
    By jackett_dad in forum Advanced Java
    Replies: 1
    Last Post: 03-16-2012, 05:53 PM
  2. Return ArrayList function - Java
    By skp123 in forum New To Java
    Replies: 23
    Last Post: 05-01-2011, 04:11 AM
  3. Filling an array from the return value of the function
    By alex1988 in forum Java Applets
    Replies: 7
    Last Post: 02-02-2011, 09:29 AM
  4. Generic return type is not inferred in for-each loop
    By ranma173 in forum Advanced Java
    Replies: 5
    Last Post: 10-08-2010, 11:50 AM
  5. getSession() Function Return Null value
    By dalchndr@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 11-04-2009, 07:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •