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 // template<class T> void throw_with_nested [[noreturn]] (T&& t);
     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 class B
     30     : public std::nested_exception
     31 {
     32     int data_;
     33 public:
     34     explicit B(int data) : data_(data) {}
     35 
     36     friend bool operator==(const B& x, const B& y) {return x.data_ == y.data_;}
     37 };
     38 
     39 int main()
     40 {
     41     {
     42         try
     43         {
     44             A a(3);
     45             std::throw_with_nested(a);
     46             assert(false);
     47         }
     48         catch (const A& a)
     49         {
     50             assert(a == A(3));
     51         }
     52     }
     53     {
     54         try
     55         {
     56             A a(4);
     57             std::throw_with_nested(a);
     58             assert(false);
     59         }
     60         catch (const std::nested_exception& e)
     61         {
     62             assert(e.nested_ptr() == nullptr);
     63         }
     64     }
     65     {
     66         try
     67         {
     68             B b(5);
     69             std::throw_with_nested(b);
     70             assert(false);
     71         }
     72         catch (const B& b)
     73         {
     74             assert(b == B(5));
     75         }
     76     }
     77     {
     78         try
     79         {
     80             B b(6);
     81             std::throw_with_nested(b);
     82             assert(false);
     83         }
     84         catch (const std::nested_exception& e)
     85         {
     86             assert(e.nested_ptr() == nullptr);
     87             const B& b = dynamic_cast<const B&>(e);
     88             assert(b == B(6));
     89         }
     90     }
     91     {
     92         try
     93         {
     94             int i = 7;
     95             std::throw_with_nested(i);
     96             assert(false);
     97         }
     98         catch (int i)
     99         {
    100             assert(i == 7);
    101         }
    102     }
    103 }
    104