Return a default value or throw an exception?

I am confused by what is considered good practice, does this language depend on decision making? Suppose I have the following Java code:

public class Stack {
  public Integer pop() {
    if (isEmpty()) return null; // or some exception maybe?
    // else get and return the top item in the stack.
  };
}

The client of the method popexpects some value Integer, and what would be the best approach so that the client knows that the stack is empty?

+4
source share
4 answers

null , . , , , - . null - , - , API , . Fail Fast

, API , , isEmpty(), .
, peek(), pop() add() - - isEmpty() API.

?

1 - null:

Integer x = stack.pop();
if (x != null) { 
  //do something with x
}

2 - :

Integer x = null;
try { 
  x = stack.pop();
} catch (MyException e) { }
//do something with x

- API.

+2

IllegalStateException.

, .

pop() - .

Optional<Integer>, Java 8 avoud null .

public class Stack {
  public Optional<Integer> pop() {
    if (isEmpty()) return Option.ofNullable(null);
    return Optional.of(valueOnTheStack);
  }
}
+2

Java API .

, , (, Java). -, .

0

API , Java-, - .

public Integer pop () throws Exception
{
    if (stack.isEmpty())
        throw new java.util.EmptyStackException();

    return stack.pop();
}

-, . , , ​​ .

StackService

@WebService
class MyStack
{
    StackProvider provider;
    Stack stack;

    public MyStack (StackProvider provider)
    {
        this.provider = provider;
        this.stack = provider.readStack();
    }

    public void sync ()
    {
        provider.syncStack(stack);
    }

    public Integer pop ()
    {
        if (stack.isEmpty())
            return new StackElement(StackElement.STACK_EMPTY);

        return new StackElement(stack.pop());
    }

    public void push (Integer val)
    {
        stack.push(val);
    }
}

StackElement

class StackElement
{
    public static final char STACK_EMPTY = 0;
    public static final char ELEMENT_VALID = 1;

    public Integer value;
    public char flag;

    public StackElement (Integer val)
    {
        this.value = val;
        this.flag = StackElement.ELEMENT_VALID;
    }

    public StackElement (char flag)
    {
        this.value = null;
        this.flag = flag;
    }

    public boolean isValid ()
    {
        return (flag == 1);
    }
}

class Test
{
    public static void main (String [] args)
    {
        StackProvider provider = new StackProvider("...");

        // init

        MyStackService service = new MyStackService(provide);
        MyStackServiceSoap soap = service.getMyStackServiceSoap();

        // call pop operation

        StackElement element = soap.pop();

        // checking the value

        if (element.isValid())
          System.out.println("Stack.Pop ~ " + element.value);
        else
        {
          if (element.flag == StackElement.STACK_EMPTY)
            System.out.println("Stack is Empty");
          else
            System.out.println("Unknown error");
        }

    }
}
0
source

All Articles