Jouni
Jouni

Reputation: 413

Is uncaught exception message guaranteed

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?

Upvotes: 1

Views: 528

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122460

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, then std::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.

Upvotes: 4

Related Questions