Debian Codegraph @

libstdc++-v3/testsuite/18_support/nested_exception/rethrow_if_nested-term.cc

raw
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target exceptions_enabled }
3
4 #include <exception>
5 #include <cstdlib>
6
7 int exit_status = 1;
8 [[noreturn]] void terminate_cleanly() noexcept { std::exit(exit_status); }
9
10 struct A { virtual ~A() = default; };
11
12 int main()
13 {
14 std::set_terminate(terminate_cleanly);
15 try
16 {
17 // At this point std::current_exception() == nullptr so the
18 // std::nested_exception object is empty.
19 std::throw_with_nested(A{});
20
21 // Should not reach this point.
22 std::abort();
23 }
24 catch (const A& a)
25 {
26 // This means the expected std::terminate() call will exit cleanly,
27 // so this test will PASS.
28 exit_status = 0;
29
30 std::rethrow_if_nested(a);
31 #if __cpp_rtti
32 // No nested exception, so trying to rethrow it calls std::terminate()
33 // which calls std::exit(0). Should not reach this point.
34 std::abort();
35 #else
36 // Without RTTI we can't dynamic_cast<const std::nested_exception*>(&a)
37 // so std::rethrow_if_nested(a) just returns normally.
38 return 0;
39 #endif
40 }
41 }