Skip to content
Snippets Groups Projects
Commit 01576ce6 authored by Nadav Har'El's avatar Nadav Har'El
Browse files

Test unhandled exception


Commit 7fc023e8 fixed issue #64, and
made unhandled exceptions work correctly.

This patch adds a test to tst-except.cc to test this - that an unhandled
exception really does call the custom termination handler that the C++
standard allows to set (before the above commit, this test would hang).

Unfortunately, gcc's libsupc++ tries very hard to ensure that the
terminination handler really does abort - if the handler returns, or
throws an exception, the library nevertheless aborts. So we need to
trick it with an ugly longjmp to make this test be able to complete
without aborting the system.

Signed-off-by: default avatarNadav Har'El <nyh@cloudius-systems.com>
parent ae5b31b8
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,8 @@
*/
#include <debug.hh>
#include <exception>
#include <setjmp.h>
int tests = 0, fails = 0;
......@@ -16,14 +18,43 @@ static void report(bool ok, const char* msg)
debug("%s: %s\n", (ok ? "PASS" : "FAIL"), msg);
}
jmp_buf env;
static bool saw_unhandled = false;
void myterminate()
{
debug("caught unhandled exception\n");
saw_unhandled = true;
longjmp(env, 1);
}
int main(int ac, char** av)
{
// Test simple throw of an integer.
try {
throw 1;
report (0, "don't continue after throw");
} catch (int e) {
report (e == 1, "catch 1");
}
// Test that unhandled exceptions work and indeed call the termination
// function as set by std::set_terminate(). Unfortunately, this test is
// very messy, as the gcc exception handling code makes very sure an
// unhandled exception aborts the system - after calling the termination
// handler, if for some reason it didn't abort, it calls abort(), and
// even catches further exceptions and aborts. So we can only escape the
// termination handler with an ugly logjmp...
auto old = std::set_terminate(myterminate);
if (setjmp(env)) {
// Second return. Success
report(saw_unhandled, "unhandled exception\n");
} else {
throw 1;
report(false, "unhandled execption\n");
}
std::set_terminate(old);
debug("SUMMARY: %d tests, %d failures\n", tests, fails);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment