{"record":{"id":"09eba99f4d67d6d8","repo":"TheAlgorithms/Python","slug":"underflow","errorCode":null,"errorMessage":"UNDERFLOW","messagePattern":"UNDERFLOW","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"data_structures/queues/circular_queue.py","lineNumber":103,"sourceCode":"\n        >>> cq = CircularQueue(5)\n        >>> cq.dequeue()\n        Traceback (most recent call last):\n           ...\n        Exception: UNDERFLOW\n        >>> cq.enqueue(\"A\").enqueue(\"B\").dequeue()\n        'A'\n        >>> (cq.size, cq.first())\n        (1, 'B')\n        >>> cq.dequeue()\n        'B'\n        >>> cq.dequeue()\n        Traceback (most recent call last):\n           ...\n        Exception: UNDERFLOW\n        \"\"\"\n        if self.size == 0:\n            raise Exception(\"UNDERFLOW\")\n\n        temp = self.array[self.front]\n        self.array[self.front] = None\n        self.front = (self.front + 1) % self.n\n        self.size -= 1\n        return temp\n","sourceCodeStart":85,"sourceCodeEnd":110,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/queues/circular_queue.py#L85-L110","documentation":"CircularQueue.dequeue raises a generic Exception('UNDERFLOW') when self.size == 0, i.e. dequeuing before any enqueue or after the queue has been drained. front/rear are modular indices over a fixed array; size is the authoritative emptiness signal.","triggerScenarios":"Two dequeues after one enqueue; dequeuing a fresh CircularQueue(n); a consumer loop that pops more items than were produced.","commonSituations":"Polling consumers without an emptiness check; misreading first()/peek semantics as consuming; porting from collections.deque.popleft (IndexError) or queue.Queue.get (blocks) and catching the wrong thing.","solutions":["Guard with 'if cq.size > 0:' or expose an is-empty check before dequeue.","Catch generic Exception and match message 'UNDERFLOW' where the empty case is legitimate.","Track produced/consumed counts on the caller side for worker loops."],"exampleFix":"# before\nitem = cq.dequeue()  # Exception: UNDERFLOW\n# after\nitem = cq.dequeue() if cq.size else None","handlingStrategy":"validation","validationCode":"item = cq.dequeue() if cq.size > 0 else None","typeGuard":null,"tryCatchPattern":"try:\n    item = cq.dequeue()\nexcept Exception as e:\n    if 'UNDERFLOW' not in str(e):\n        raise\n    item = None","preventionTips":["Check cq.size > 0 before dequeuing; size is the authoritative emptiness signal.","Generic Exception means you must match the message text.","Pair every producer increment with at most one consume."],"tags":["queue","underflow","circular-buffer","empty-collection"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}