小编Hec*_*orJ的帖子

终止在抛出异常实例后调用,核心转储

我正在讨论C++异常并遇到一个错误,我不确定它为什么会给我一些问题:

 #include <iostream>
 #include <exception>

 class err : public std::exception
 {
 public:
      const char* what() const noexcept { return "error"; }
 };

 void f() throw()
 {
      throw err();
 }

 int main()
 {
      try
      {
           f();
      }
      catch (const err& e)
      {
           std::cout << e.what() << std::endl;
      }
 }
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到以下运行时错误:

 terminate called after throwing an instance of 'err'
   what():  error
 Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)

如果我try/catch完全移动逻辑f(),即

 void f() 
 {
      try
      {
           throw err();
      }
      catch (const err& e)
      { …
Run Code Online (Sandbox Code Playgroud)

c++ exception c++11

12
推荐指数
1
解决办法
2583
查看次数

标签 统计

c++ ×1

c++11 ×1

exception ×1