Why Java provides two methods to remove element from Queue?

munish picture munish · Feb 3, 2010 · Viewed 25.9k times · Source

The Queue implementation in Java has two methods to remove element, One is remove() which throws exception and other one is poll() which returns null for an empty queue. I have two doubts:

  1. Why Queue has different implementation to remove element?
  2. Which implementation to use When?

Answer

Theo picture Theo · Feb 3, 2010

In some situations it's expected that a queue will be empty, and in those cases having a method that doesn't throw an exception is appropriate. In other situations it's an exceptional circumstance that the queue is empty, and an exception is appropriate.

Throwing exceptions incurs a performance penalty, and if it's the case that you expect the queue to be empty from time to time you don't want to have to handle the queue-empty-logic as catching an exception -- it's both costly and difficult to read.

In the opposite case where you don't expect the queue to ever be empty it is a sign of a programming error, or some other exceptional circumstance that it is, and you don't want to write ugly error condition checking code (e.g. checking for null), because in this case that would be less readable than catching an exception (which you can do in another scope).