【解决】VsCode C++异常【terminate called after throwing an instance of ‘char const‘】

大纲

在写栈的相关代码时,使用了 throw 抛异常,但是异常没有抛成功,命令行出现了下面的内容。

问题解决

原代码为

cpp 复制代码
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 即问题解决。

cpp 复制代码
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;
}

问题解决正确抛出异常

相关推荐
不想写代码的星星21 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054965 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django