std::runtime_error 异常传递失败
来源:互联网 发布:学完c语言 编辑:程序博客网 时间:2024/06/14 10:43
最近开始学习使用cpp的异常。有这样一个使用场景,函数A可能会抛出异常,函数B调用函数A,捕获到A抛出的异常后,做一定的处理,再把这个异常往上抛。main函数调用函数B,捕获到B抛出的异常,但内容却是不对的。
但是这样不能解决我的问题,A函数可能会抛出其他类型异常,B里本来捕获异常类型为exception,exception的派生类异常(如runtime_error,range_error)都能捕捉到,现在改成runtime_error类型就只能捕捉这种异常。
刚刚又网上搜了下,发现把函数B的catch中的
void B() { try { A(); } catch (exception & e) { cout << e.what() << endl; throw e; }}int main() { try { B(); } catch (exception & e) {cout << e.what() << endl; } return 0;}如上代码,B函数里能输出异常,main函数 e.what(); 输出是“std::exception”。后来把函数B的catch的异常参数类型改为 runtime_error,就符合预期了,main函数也能输出"hello world"。百思不得其解。
但是这样不能解决我的问题,A函数可能会抛出其他类型异常,B里本来捕获异常类型为exception,exception的派生类异常(如runtime_error,range_error)都能捕捉到,现在改成runtime_error类型就只能捕捉这种异常。
刚刚又网上搜了下,发现把函数B的catch中的
throw e;改为
throw;就符合预期了,函数B的catch的异常参数类型还是exception。这个语法真是醉了。。。
0 0
- std::runtime_error 异常传递失败
- c++中异常处理中:logic_error、runtime_error
- C++/C++11中std::runtime_error的使用
- std::vector传递dll参数异常-长度超限
- std 异常
- 解决问题: terminate called after throwing an instance of 'std::runtime_error' what(): locale::facet::
- 在roscore时出现 terminate called after throwing an instance of 'std::runtime_error'的解决方法
- C++之用std::nothrow分配内存失败不抛异常
- ubuntu12.04连接服务器的时候出现错误“terminate called after throwing an instance of 'std::runtime_error' what(”
- std::map 编译异常
- std vector传递指针使用说明
- 跨DLL传递std::string
- 参数传递&异常传递
- osg::readNodeFile() 异常std::bab_alloc
- std::out_of_range 抛出抛出异常
- 编程中的runtime_error问题
- Activity传递数据失败
- 遭遇std::bad_alloc,内存分配失败
- SDUT 3363 数据结构实验之图论七:驴友计划
- c++ 概念和定义
- Docker1.13.1在 Ubuntu 16.04 上的安装和配置
- 我眼中的设计模式(2)
- Java-日志
- std::runtime_error 异常传递失败
- 简单工厂模式(静态工厂方法(Static Factory Method)模式)
- 并查集代码总结
- 伙伴系统的概述
- LeetCode 3. Longest Substring Without Repeating Characters
- VMWare 安装 Eclipse
- 【event】tx:allocate ITL entry
- JSP内置对象response常见用法
- 传统定时器Timer