TheAlgorithms/Python · error · Exception

Empty Queue

Error message

Empty Queue

What it means

CircularQueueLinkedList.check_can_perform_operation raises a generic Exception('Empty Queue') when front == rear and front.data is None. It is invoked by first() and dequeue(), making any read or removal on an empty queue fail with this message.

Source

Thrown at data_structures/queues/circular_queue_linked_list.py:144

        Exception: Empty Queue
        """
        self.check_can_perform_operation()
        if self.rear is None or self.front is None:
            return None
        if self.front == self.rear:
            data = self.front.data
            self.front.data = None
            return data

        old_front = self.front
        self.front = old_front.next
        data = old_front.data
        old_front.data = None
        return data

    def check_can_perform_operation(self) -> None:
        if self.is_empty():
            raise Exception("Empty Queue")

    def check_is_full(self) -> None:
        if self.rear and self.rear.next == self.front:
            raise Exception("Full Queue")


class Node:
    def __init__(self) -> None:
        self.data: Any | None = None
        self.next: Node | None = None
        self.prev: Node | None = None


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Call cq.is_empty() before first()/dequeue().
  2. Catch generic Exception and match 'Empty Queue' since no dedicated exception type exists.
  3. In drain loops use 'while not cq.is_empty(): process(cq.dequeue())'.

Example fix

# before
item = cq.dequeue()  # Exception: Empty Queue
# after
item = None if cq.is_empty() else cq.dequeue()
Defensive patterns

Strategy: validation

Validate before calling

item = cq.dequeue() if not cq.is_empty() else None
first = cq.first() if not cq.is_empty() else None

Try / catch

try:
    item = cq.dequeue()
except Exception as e:
    if 'Empty Queue' not in str(e):
        raise
    item = None

Prevention

When it happens

Trigger: first() or dequeue() on a freshly built CircularQueueLinkedList; dequeuing after all enqueued items were consumed; extra dequeue after a drain loop.

Common situations: Peek-before-check patterns; consumers assuming blocking semantics; capacity set to 1 confusing the empty check (front==rear with data None) for a single-slot queue.

Related errors


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