【解决】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;
}

问题解决正确抛出异常

相关推荐
晨非辰40 分钟前
《剑指Offer:单链表操作入门——从“头删”开始破解面试》
c语言·开发语言·数据结构·c++·笔记·算法·面试
sheji34164 小时前
【开题答辩全过程】以 python杭州亚运会数据分析与可视化开题为例,包含答辩的问题和答案
开发语言·python·数据分析
渡我白衣5 小时前
list 与 forward_list:一场 STL 中的“链表哲学”之争
数据结构·c++·list
weixin_446260857 小时前
快速构建网站的利器——Symfony PHP框架
开发语言·php·symfony
王夏奇7 小时前
C语言中#pragma的用法
c语言·开发语言
charlie1145141918 小时前
理解C++20的革命特性——协程支持2:编写简单的协程调度器
c++·学习·算法·设计模式·c++20·协程·调度器
李宥小哥8 小时前
C#基础10-结构体和枚举
java·开发语言·c#
带娃的IT创业者8 小时前
第4集:配置管理的艺术:环境变量、多环境配置与安全实践
开发语言·python·安全·项目配置·开发基础
省四收割者9 小时前
Go语言入门(20)-nil
开发语言·vscode·golang
19岁开始学习9 小时前
Go语言中的Zap日志库
开发语言·golang·xcode