terminate-handler

set_terminate function is not working for me

我是研究僧i 提交于 2021-02-16 06:25:30
问题 I have the following code taken from cplusplus.com: // set_terminate example #include <iostream> #include <exception> #include <cstdlib> using namespace std; void myterminate () { cout << "terminate handler called\n"; abort(); // forces abnormal termination } int main (void) { set_terminate (myterminate); throw 0; // unhandled exception: calls terminate handler return 0; } As there is unhandled exception in the code, it needs to call myterminate() function which is set as terminate handler

Must user provided terminate() function be thread-safe?

好久不见. 提交于 2020-01-03 07:20:08
问题 As stated in http://en.cppreference.com/w/cpp/error/terminate there are many reasons to call terminate. I can imagine case where just almost in the same time some of these reasons happen in two threads. Q1 Can the terminate function set by std::set_terminate be called twice or more at the same time, by the same time I mean second call begins before first has ended. Thread1 Thread2 | | _ | t | e | r | m | i _ n t a e t r e m - ? Q2 If Q1==YES, then what happens if first terminate ended. I

Can a terminate handler throw an exception?

对着背影说爱祢 提交于 2019-12-21 05:06:48
问题 What is the defined behavior of the following program, if any? #include <iostream> #include <exception> #include <cstdlib> void i_throw() { std::cout << "i_throw()" << std::endl; // std::terminate() is noexcept so if the terminate handler throws... // then the terminate handler is called... // std::terminate is [[noreturn]] so don't return try { throw 7; } catch(...) { std::cout << "caught exception, re-throw()-ing" << std::endl; throw; } std::cout << "got here!" << std::endl; std::abort(); }

Can a terminate handler throw an exception?

我的未来我决定 提交于 2019-12-03 14:16:15
What is the defined behavior of the following program, if any? #include <iostream> #include <exception> #include <cstdlib> void i_throw() { std::cout << "i_throw()" << std::endl; // std::terminate() is noexcept so if the terminate handler throws... // then the terminate handler is called... // std::terminate is [[noreturn]] so don't return try { throw 7; } catch(...) { std::cout << "caught exception, re-throw()-ing" << std::endl; throw; } std::cout << "got here!" << std::endl; std::abort(); } int main() { std::set_terminate(i_throw); throw; std::terminate(); } With gcc and clang I get the