TheAlgorithms/Python · error · UnderFlowError

All queues are empty

Error message

All queues are empty

What it means

Raised by FixedPriorityQueue.dequeue() (data_structures/queues/priority_queue_using_list.py:97) when all three priority buckets are empty. UnderFlowError is a custom exception defined in this module (subclassing Exception), not a builtin, so a bare `except Exception` or importing UnderFlowError from the module is needed to catch it specifically.

Source

Thrown at data_structures/queues/priority_queue_using_list.py:97

        If the priority is invalid ValueError is raised.
        If the queue is full an OverFlowError is raised.
        """
        try:
            if len(self.queues[priority]) >= 100:
                raise OverflowError("Maximum queue size is 100")
            self.queues[priority].append(data)
        except IndexError:
            raise ValueError("Valid priorities are 0, 1, and 2")

    def dequeue(self) -> int:
        """
        Return the highest priority element in FIFO order.
        If the queue is empty then an under flow exception is raised.
        """
        for queue in self.queues:
            if queue:
                return queue.pop(0)
        raise UnderFlowError("All queues are empty")

    def __str__(self) -> str:
        return "\n".join(f"Priority {i}: {q}" for i, q in enumerate(self.queues))


class ElementPriorityQueue:
    """
    Element Priority Queue is the same as Fixed Priority Queue except that the value of
    the element itself is the priority. The rules for priorities are the same the as
    Fixed Priority Queue.

    >>> epq = ElementPriorityQueue()
    >>> epq.enqueue(10)
    >>> epq.enqueue(70)
    >>> epq.enqueue(4)
    >>> epq.enqueue(1)
    >>> epq.enqueue(5)
    >>> epq.enqueue(7)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check emptiness first: all(len(q) == 0 for q in pq.queues) before dequeue()
  2. Catch the module's UnderFlowError (from queues.priority_queue_using_list import UnderFlowError) in consumer loops
  3. Use a sentinel: enqueue a shutdown marker per priority instead of dequeuing past the end

Example fix

// before
item = pq.dequeue()  # UnderFlowError when drained

# after
if any(pq.queues):
    item = pq.dequeue()
else:
    item = None
Defensive patterns

Strategy: validation

Validate before calling

if all(len(q) == 0 for q in pq.queues):
    return None  # nothing to dequeue
item = pq.dequeue()

Try / catch

from data_structures.queues.priority_queue_using_list import UnderFlowError
try:
    item = pq.dequeue()
except UnderFlowError:
    item = None

Prevention

When it happens

Trigger: Calling dequeue() on a freshly constructed FixedPriorityQueue, or after dequeuing every element previously enqueued across all three priorities.

Common situations: Worker loops that drain the queue in a while True loop, or code that pre-checks only one bucket (e.g. queues[0]) before calling dequeue() and misses items sitting in other buckets being absent too.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/a07189a119983457. Report an issue: GitHub.