Is the following code
#include <stdexcept>
int main() {
throw std::runtime_error("foobar");
}
guaranteed to produce the following outout?
terminate called after throwing an instance of 'std::runtime_error'
what(): foobar
fish: Job 1, './a.out' terminated by signal SIGABRT (Abort)
Can I rely on this exact output on every compiler?
Can I rely on that what
method will be called and error message will be printed will be printed?
CodePudding user response:
No it is not guaranteed, it unspecified whether there is any message. From cppreference:
If an exception is thrown and not caught, including exceptions that escape the initial function of
std::thread
, the main function, and the constructor or destructor of any static or thread-local objects, thenstd::terminate
is called. It is implementation-defined whether any stack unwinding takes place for uncaught exceptions.
The case that is relevant here is "exceptions that escape [...] the main function". The call to std::terminate
is guaranteed though. And you can install a std::terminate_handler
to print a custom message if you like.