static_rtti
static_rtti

Reputation: 56282

Why do unhandled exceptions cause a segmentation fault?

Here is a minimum example:

[joel@maison various] (master *)$ cat throw.cpp 

#include <iostream>


int main(int argc, char* argv[])
{
  throw("pouet pouet");
}

[joel@maison various] (master *)$ ./a.out 
terminate called after throwing an instance of 'char const*'
Aborted (core dumped)

Reading the docs, it seems like the default terminate handler is abort(). I couldn't find anything about triggering a segfault in the abort man page.

Upvotes: 1

Views: 1295

Answers (1)

Marco A.
Marco A.

Reputation: 43662

Throwing an exception and not handling it calls abort() which raises SIGABRT.

You can verify it with the following

#include <iostream>
#include <stdexcept>
#include <signal.h>

extern "C" void handle_sigabrt(int)
{
    std::cout << "Handling and then returning (exiting)" << std::endl;
}

int main()
{
  signal(SIGABRT, &handle_sigabrt);

  throw("pouet pouet");
}

Demo

Upvotes: 7

Related Questions