{"record":{"id":"90110a30739f00b0","repo":"krahets/hello-algo","slug":"error-90110a","errorCode":null,"errorMessage":"佇列為空","messagePattern":"佇列為空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"zh-hant/codes/javascript/chapter_stack_and_queue/array_queue.js","lineNumber":57,"sourceCode":"        // 透過取餘操作實現 rear 越過陣列尾部後回到頭部\n        const rear = (this.#front + this.size) % this.capacity;\n        // 將 num 新增至佇列尾\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* 出列 */\n    pop() {\n        const num = this.peek();\n        // 佇列首指標向後移動一位，若越過尾部，則返回到陣列頭部\n        this.#front = (this.#front + 1) % this.capacity;\n        this.#queSize--;\n        return num;\n    }\n\n    /* 訪問佇列首元素 */\n    peek() {\n        if (this.isEmpty()) throw new Error('佇列為空');\n        return this.#nums[this.#front];\n    }\n\n    /* 返回 Array */\n    toArray() {\n        // 僅轉換有效長度範圍內的串列元素\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n            arr[i] = this.#nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n\n/* Driver Code */\n/* 初始化佇列 */\nconst capacity = 10;\nconst queue = new ArrayQueue(capacity);","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/javascript/chapter_stack_and_queue/array_queue.js#L39-L75","documentation":"Thrown by ArrayQueue.peek() (message: '佇列為空' = 'queue is empty') when the queue has no elements. peek() returns nums[front]; without the guard it returns stale/undefined data. pop() delegates to peek(), so pop on an empty queue also triggers this.","triggerScenarios":"Calling pop() or peek() on an empty queue, or dequeuing more items than were enqueued.","commonSituations":"Consumer loop draining faster than producer; FIFO processing with no guard; stale front pointer after capacity reset.","solutions":["Check queue.isEmpty() before pop() or peek().","Use while (queue.size > 0) for drain loops.","If peeking for display, guard with isEmpty() and return null or a placeholder."],"exampleFix":"// before\nconst val = queue.pop(); // throws '佇列為空' if empty (pop calls peek)\n\n// after\nwhile (queue.size > 0) {\n    const val = queue.pop();\n}","handlingStrategy":"validation","validationCode":"// peek() and pop() (which calls peek) both throw on empty\nif (queue.size > 0) {\n    const val = queue.pop();\n}","typeGuard":null,"tryCatchPattern":"try {\n    const val = queue.pop();\n} catch (e) {\n    if (e.message === '佇列為空') {\n        // queue is empty — handle underflow\n    } else throw e;\n}","preventionTips":["Check queue.size > 0 before pop() or peek().","Use while (queue.size > 0) for drain loops.","Remember pop() internally calls peek(), so both need the guard."],"tags":["queue","javascript","empty-state","precondition"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}