{"record":{"id":"74680344fcd8c1cd","repo":"krahets/hello-algo","slug":"error-746803","errorCode":null,"errorMessage":"栈为空","messagePattern":"栈为空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"codes/javascript/chapter_stack_and_queue/array_stack.js","lineNumber":31,"sourceCode":"\n    /* 获取栈的长度 */\n    get size() {\n        return this.#stack.length;\n    }\n\n    /* 判断栈是否为空 */\n    isEmpty() {\n        return this.#stack.length === 0;\n    }\n\n    /* 入栈 */\n    push(num) {\n        this.#stack.push(num);\n    }\n\n    /* 出栈 */\n    pop() {\n        if (this.isEmpty()) throw new Error('栈为空');\n        return this.#stack.pop();\n    }\n\n    /* 访问栈顶元素 */\n    top() {\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/codes/javascript/chapter_stack_and_queue/array_stack.js#L13-L49","documentation":"Thrown by pop() on an array-backed stack (ArrayStack) when the stack is empty (internal #stack.length === 0). pop() calls the native Array.pop after the guard; without the check, Array.pop would return undefined and the caller could mistake that for a value. This guard makes underflow explicit.","triggerScenarios":"Calling pop() on a freshly constructed stack; popping more than was pushed; mismatched push/pop in a balanced-parentheses or DFS routine.","commonSituations":"Expression evaluation (too many operators); backtracking/DFS unwind past the start; reversing with push-all then pop-too-many.","solutions":["Check stack.isEmpty() before pop(): if (!stack.isEmpty()) stack.pop();.","Use a counted pop tied to a recorded push count.","In DFS, only pop a frame you pushed; assert the stack is non-empty before processing.","Prefer returning a sentinel (e.g. undefined or null) via a wrapper instead of relying on the throw for control flow."],"exampleFix":"// before\nconst top = stack.pop(); // throws if empty\n// after\nconst top = stack.isEmpty() ? null : stack.pop();","handlingStrategy":"validation","validationCode":"function safePop(stack) {\n  return stack.isEmpty() ? null : stack.pop();\n}","typeGuard":"function stackNotEmpty(stack) {\n  return !stack.isEmpty();\n}","tryCatchPattern":"try {\n  const x = stack.pop();\n} catch (e) {\n  if (e instanceof Error && e.message === '栈为空') { /* underflow */ } else throw e;\n}","preventionTips":["Check stack.isEmpty() before pop().","Only pop frames you pushed in DFS/backtracking.","Use a counted pop tied to a recorded push count.","Wrap pop() in a helper returning a sentinel on empty."],"tags":["stack","empty-state","javascript","array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}