std::abort
STD:中止
Defined in header | | |
---|---|---|
void abort( | | (until C++11) |
[noreturn] void abort( | | (since C++11) |
导致异常程序终止,除非SIGABRT
被传递给信号的信号处理程序捕获,而处理程序不返回。
具有自动、线程本地和静态变量的析构函数储存期限都不叫。传递给std::atexit()
也不叫。是否关闭文件等开放资源是实现定义的。将实现定义状态返回给表示执行失败的主机环境。
参数
%280%29
返回值
%280%29
例外
(none) | (until C++11) |
---|---|
noexcept specification: noexcept | (since C++11) |
例
二次
#include <csignal>
#include <iostream>
#include <cstdlib>
class Tester {
public:
Tester() { std::cout << "Tester ctor\n"; }
~Tester() { std::cout << "Tester dtor\n"; }
};
Tester static_tester; // Destructor not called
void signal_handler(int signal)
{
if (signal == SIGABRT) {
std::cerr << "SIGABRT received\n";
} else {
std::cerr << "Unexpected signal " << signal << " received\n";
}
std::_Exit(EXIT_FAILURE
}
int main()
{
Tester automatic_tester; // Destructor not called
// Setup handler
auto previous_handler = std::signal(SIGABRT, signal_handler
if (previous_handler == SIG_ERR) {
std::cerr << "Setup failed\n";
return EXIT_FAILURE;
}
std::abort( // Raise SIGABRT
std::cout << "This code is unreachable\n";
}
二次
产出:
二次
Tester ctor
Tester ctor
SIGABRT received
二次
另见
exit | causes normal program termination with cleaning up (function) |
---|---|
atexit | registers a function to be called on std::exit() invocation (function) |
quick_exit (C++11) | causes quick program termination without completely cleaning up (function) |
signal | sets a signal handler for particular signal (function) |
c中止的文件
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。