有时我们无法确定一段代码会抛出何种异常,但是又希望能捕获这些异常
在不能确定异常类型时,首先应该尝试捕获std::exception, 通过what()方法可以得到一个较为详细的报错信息。
但有时,也可能会遇到一些不按套路出牌的代码,这时就需要使用catch(...)来进行捕获,通过__cxa_exception_type可以大致确定报错的类型,之后再修改代码进行进一步的处理
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | #include <exception>#include <typeinfo>
 #include <stdexcept>
 
 try {
 
 } catch (const std::exception &e) {
 const char* describe = e.what();
 
 } catch(...) {
 std::exception_ptr p = std::current_exception();
 std::string type_name = (p ? p.__cxa_exception_type()->name() : "null");
 
 }
 
 |