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

问题解决正确抛出异常

相关推荐
你怎么知道我是队长3 小时前
C语言---循环结构
c语言·开发语言·算法
老赵的博客3 小时前
c++ unqiue指针
java·jvm·c++
o0o_-_4 小时前
【go/gopls/mcp】官方gopls内置mcp server使用
开发语言·后端·golang
Dxy12393102164 小时前
python把文件从一个文件复制到另一个文件夹
开发语言·python
程序猿编码4 小时前
基于 Linux 内核模块的字符设备 FIFO 驱动设计与实现解析(C/C++代码实现)
linux·c语言·c++·内核模块·fifo·字符设备
怎么没有名字注册了啊4 小时前
MFC_Install_Create
c++·mfc
酷飞飞4 小时前
Qt Designer与事件处理
开发语言·qt·命令模式
天雪浪子5 小时前
Python入门教程之赋值运算符
开发语言·python
Wadli5 小时前
C++语法 | static静态|单例模式
开发语言·c++·单例模式
他们都不看好你,偏偏你最不争气5 小时前
【iOS】AFNetworking
开发语言·macos·ios·objective-c