Difference between revisions of "cpp/utility/program/abort"
From cppreference.com
(add reference to signal function) |
(add example) |
||
Line 30: | Line 30: | ||
| | | | ||
| code= | | code= | ||
+ | #include <cassert> // assert | ||
+ | #include <csignal> // std::signal | ||
+ | #include <iostream> // std::cout | ||
+ | |||
+ | class Tester { | ||
+ | public: | ||
+ | Tester() { | ||
+ | std::cout << "Tester ctor\n"; | ||
+ | } | ||
+ | |||
+ | ~Tester() { | ||
+ | std::cout << "Tester dtor\n"; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | // Destructor not called | ||
+ | Tester static_tester; | ||
+ | |||
+ | 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() { | ||
+ | // Destructor not called | ||
+ | Tester automatic_tester; | ||
+ | |||
+ | // Setup handler | ||
+ | const auto previous_handler = std::signal(SIGABRT, signal_handler); | ||
+ | if (previous_handler == SIG_ERR) { | ||
+ | std::cerr << "Setup failed\n"; | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | |||
+ | // Raise SIGABRT | ||
+ | assert(false); | ||
+ | |||
+ | std::cout << "This code is unreachable" << std::endl; | ||
+ | return EXIT_SUCCESS; | ||
+ | } | ||
| output= | | output= | ||
+ | Tester ctor | ||
+ | Tester ctor | ||
+ | Assertion failed: (false), function main, file ..., line 41. | ||
+ | SIGABRT received | ||
}} | }} | ||
Revision as of 08:30, 18 July 2013
Defined in header <cstdlib>
|
||
void abort(); |
(until C++11) | |
[[noreturn]] void abort(); |
(since C++11) | |
Causes abnormal program termination unless SIGABRT
is being caught by a signal handler passed to signal and the handler does not return.
Destructors of variables with automatic, thread local and static storage durations are not called. Functions, passed to atexit()
are also not called. Whether open resources such as files are closed is implementation defined. Implementation defined status is returned to the host environment that indicates unsuccessful execution.
Contents |
Parameters
(none)
Return value
(none)
Exceptions
noexcept specification:
noexcept
Example
Run this code
#include <cassert> // assert #include <csignal> // std::signal #include <iostream> // std::cout class Tester { public: Tester() { std::cout << "Tester ctor\n"; } ~Tester() { std::cout << "Tester dtor\n"; } }; // Destructor not called Tester static_tester; 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() { // Destructor not called Tester automatic_tester; // Setup handler const auto previous_handler = std::signal(SIGABRT, signal_handler); if (previous_handler == SIG_ERR) { std::cerr << "Setup failed\n"; return EXIT_FAILURE; } // Raise SIGABRT assert(false); std::cout << "This code is unreachable" << std::endl; return EXIT_SUCCESS; }
Output:
Tester ctor Tester ctor Assertion failed: (false), function main, file ..., line 41. SIGABRT received
See also
causes normal program termination with cleaning up (function) | |
registers a function to be called on std::exit() invocation (function) | |
(C++11) |
causes quick program termination without completely cleaning up (function) |
sets a signal handler for particular signal (function) | |
C documentation for abort
|