【发布时间】:2018-09-27 14:25:55
【问题描述】:
我尝试在我的代码中捕获一个终止信号,以便在退出之前编写一个重启文件。我的解决方案基于此answer。
#include <exception>
#include <csignal>
#include <iostream>
class InterruptException : public std::exception
{
public:
InterruptException(int _s) : signal_(_s) { }
int signal() const noexcept
{
return this->signal_;
}
private:
int signal_;
};
/// method to throw exception at signal interrupt
void sig_to_exception(int s)
{
throw InterruptException(s);
}
int main()
{
// activate signal handling
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = sig_to_exception;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
try
{
for (std::size_t i = 0; i < 100000000; ++i)
{
std::cout << i << std::endl;
}
}
catch (const InterruptException& e)
{
std::cout << "Received signal " << e.signal() << std::endl;
std::exit(1);
}
catch(...)
{
std::cout << "Other catch!" << std::endl;
}
}
异常被抛出,但是,我的 catch 块没有捕获它。程序以未捕获的异常InterruptException 终止。我在 MacOS 上尝试了 clang 和 gcc。知道为什么没有正确捕获异常吗?
谢谢
使用 g++ 7.3.0 编译时的输出:
terminate called after throwing an instance of 'InterruptException'
what(): std::exception
Abort trap: 6
使用 Apple LLVM 9.0.0 编译时的输出
libc++abi.dylib: terminating with uncaught exception of type InterruptException: std::exception
PS:当我使用 Apple LLVM 编译时,似乎有时会捕获异常,但并非总是如此,这使得这更加奇怪。
【问题讨论】:
-
一个问题(我不知道答案)是在与应用程序的主线程相同的线程上引发的信号吗?如果不是,则用于抛出的 C++ 异常的堆栈将永远不会展开到捕获。
-
我在我的代码中使用 OpenMP,但上面的示例是串行的并且显示了相同的行为。
-
阅读:stackoverflow.com/questions/22005719/… ...进程中哪个线程将处理此信号尚未确定..."
标签: c++ exception exception-handling signals