编程题中边界情况是否要用 exception?
编程题中边界情况是否要用 exception? 例如用两个栈来实现一个队列, 以下代码在两个栈里面都没有数字时抛出一个异常,在面试题中是否合适?在实际产品中是否合适?
class Solution { public: void push(int node) { stack1.push(node); } int pop() { if (stack2.empty()){ if (stack1.empty()) throw "no number available"; while (!stack1.empty()){ stack2.push(stack1.top()); stack1.pop(); } } int result = stack2.top(); stack2.pop(); return result; } private: stack<int> stack1; stack<int> stack2; };