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

问题解决正确抛出异常

相关推荐
Chris _data16 分钟前
并发单词频率统计器 - 从零到完整实现(C# 实战)
开发语言·c#
idolao17 分钟前
Oligo 7.60 安装教程:引物设计+Java 环境配置
java·开发语言
不知名的老吴22 分钟前
Lambda表达式与新的Streams API相结合
开发语言·python
王老师青少年编程7 小时前
信奥赛C++提高组csp-s之搜索进阶(搜索剪枝案例实践1)
c++·csp·高频考点·信奥赛·提高组·搜索剪枝·小木棍
石山代码7 小时前
ArrayList / HashMap / ConcurrentHashMap
java·开发语言
程序大视界7 小时前
【Python系列课程】Python正则表达式(下):环视、命名分组与日志实战
开发语言·python·正则表达式
枫叶v.8 小时前
Agent 分层存储架构设计:从记忆方法到中间件选型
开发语言·python
sleven fung9 小时前
MinerU与BabelDOC与KTransformers与OpenAI API库
开发语言·python·ai·langchain
萤萤七悬9 小时前
【Python笔记】AI帮实现CLI工具-使用argparse.ArgumentParser接收命令参数
开发语言·笔记·python
iCxhust10 小时前
C# 命令行指令 查看二进制文件
开发语言·单片机·嵌入式硬件·c#·proteus·微机原理·8088单板机