Home | History | Annotate | Download | only in except.nested
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <exception>
     11 
     12 // class nested_exception;
     13 
     14 // void rethrow_nested [[noreturn]] () const;
     15 
     16 #include <exception>
     17 #include <cstdlib>
     18 #include <cassert>
     19 
     20 class A
     21 {
     22     int data_;
     23 public:
     24     explicit A(int data) : data_(data) {}
     25 
     26     friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
     27 };
     28 
     29 void go_quietly()
     30 {
     31     std::exit(0);
     32 }
     33 
     34 int main()
     35 {
     36     {
     37         try
     38         {
     39             throw A(2);
     40             assert(false);
     41         }
     42         catch (const A&)
     43         {
     44             const std::nested_exception e;
     45             assert(e.nested_ptr() != nullptr);
     46             try
     47             {
     48                 e.rethrow_nested();
     49                 assert(false);
     50             }
     51             catch (const A& a)
     52             {
     53                 assert(a == A(2));
     54             }
     55         }
     56     }
     57     {
     58         try
     59         {
     60             std::set_terminate(go_quietly);
     61             const std::nested_exception e;
     62             e.rethrow_nested();
     63             assert(false);
     64         }
     65         catch (...)
     66         {
     67             assert(false);
     68         }
     69     }
     70 }
     71