{"record":{"id":"0278839f52045630","repo":"krahets/hello-algo","slug":"error-027883","errorCode":null,"errorMessage":"堆疊為空","messagePattern":"堆疊為空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"zh-hant/codes/typescript/chapter_stack_and_queue/array_stack.ts","lineNumber":31,"sourceCode":"\n    /* 獲取堆疊的長度 */\n    get size(): number {\n        return this.stack.length;\n    }\n\n    /* 判斷堆疊是否為空 */\n    isEmpty(): boolean {\n        return this.stack.length === 0;\n    }\n\n    /* 入堆疊 */\n    push(num: number): void {\n        this.stack.push(num);\n    }\n\n    /* 出堆疊 */\n    pop(): number | undefined {\n        if (this.isEmpty()) throw new Error('堆疊為空');\n        return this.stack.pop();\n    }\n\n    /* 訪問堆疊頂元素 */\n    top(): number | undefined {\n        if (this.isEmpty()) throw new Error('堆疊為空');\n        return this.stack[this.stack.length - 1];\n    }\n\n    /* 返回 Array */\n    toArray() {\n        return this.stack;\n    }\n}\n\n/* Driver Code */\n/* 初始化堆疊 */\nconst stack = new ArrayStack();","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/typescript/chapter_stack_and_queue/array_stack.ts#L13-L49","documentation":"An Error '堆疊為空' ('stack is empty') thrown by pop() in ArrayStack (array_stack.ts:31). pop() delegates to the native Array.pop after an emptiness check; without the guard, popping an empty JS array returns undefined, which the typed number return would mask as a logic bug. The check makes the failure explicit.","triggerScenarios":"Calling stack.pop() when stack.isEmpty() is true (this.stack.length === 0). Popping more times than you pushed, or popping a freshly constructed `new ArrayStack()`.","commonSituations":"Mismatched push/pop counts; unbalanced parentheses/bracket matching that pops on a close token with nothing on the stack; DFS/undo implementations that pop without checking depth; reusing a stack instance after draining it.","solutions":["Check stack.isEmpty() before pop.","Loop with `while (!stack.isEmpty())` when draining.","Track the number of pushes and never pop more than that.","Wrap pop in a helper returning undefined when empty if that is the desired contract."],"exampleFix":"// before: popping an empty stack throws\nconst top = stack.pop();\n\n// after: guard with emptiness check\nconst top = stack.isEmpty() ? undefined : stack.pop();","handlingStrategy":"validation","validationCode":"// Guard ArrayStack pop\nfunction safePop(stack: ArrayStack): number | undefined {\n    return stack.isEmpty() ? undefined : stack.pop();\n}\nwhile (!stack.isEmpty()) {\n    const top = stack.pop();\n}","typeGuard":"const nonEmpty = (stack: ArrayStack): boolean => !stack.isEmpty();","tryCatchPattern":"try {\n    const top = stack.pop();\n} catch (e) {\n    if (e instanceof Error && e.message === '堆疊為空') {\n        // stack empty; handle gracefully\n    } else throw e;\n}","preventionTips":["Check isEmpty() before pop.","Track push count and never pop more than that.","Drain with `while (!stack.isEmpty())`.","Reset shared stack instances between runs."],"tags":["typescript","stack","array","empty-collection"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}