小编sam*_*sam的帖子

为什么这段代码会阻塞不执行?

catch处理程序未运行.但为什么?

如果在块thread t之前启动try,则捕获处理程序将运行.

如果catch块的类型与抛出的类型不匹配,程序将退出,说明线程以未捕获的异常终止,表明异常已处理,但catch块未运行.

#include <iostream>
#include <thread>

using namespace std;

void do_work() {}

int main() {
  std::cerr << "RUNNING" << std::endl;
  try {
    thread t(do_work);
    std::cerr << "THROWING" << std::endl;
    throw logic_error("something went wrong");
  } catch (logic_error e) {
    std::cerr << "GOTCHA" << std::endl;
  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译命令:

c++ -std=c++14 -pthread -pedantic -Wall -Wextra -O0 scratch.cpp -o scratch
Run Code Online (Sandbox Code Playgroud)

c++ error-handling multithreading try-catch

7
推荐指数
1
解决办法
246
查看次数

标签 统计

c++ ×1

error-handling ×1

multithreading ×1

try-catch ×1