大纲
在写栈的相关代码时,使用了 throw 抛异常,但是异常没有抛成功,命令行出现了下面的内容。
问题解决
原代码为
int main(){
ArrayStack stack;
stack.push(1);
stack.push(4);
stack.push(6);
while(!stack.empty()){
cout << stack.top() << " ";
stack.pop();
}
cout << endl;
cout<< "size " << stack.size()<<endl;
stack.pop();
cout << stack.top() << endl;
return 0;
}
修改后的代码,增加try catch 即问题解决。
int main(){
ArrayStack stack;
stack.push(1);
stack.push(4);
stack.push(6);
while(!stack.empty()){
cout << stack.top() << " ";
stack.pop();
}
cout << endl;
cout<< "size " << stack.size()<<endl;
try{
stack.pop();
cout << stack.top() << endl;
}catch(char const *str){
cout<< str << endl;
}
return 0;
}
问题解决正确抛出异常