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 E> void rethrow_if_nested(const E& e); 15 16 #include <exception> 17 #include <cstdlib> 18 #include <cassert> 19 20 class A 21 { 22 int data_; 23 public: A(int data)24 explicit A(int data) : data_(data) {} ~A()25 virtual ~A() _NOEXCEPT {} 26 operator ==(const A & x,const A & y)27 friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;} 28 }; 29 30 class B 31 : public std::nested_exception, 32 public A 33 { 34 public: B(int data)35 explicit B(int data) : A(data) {} B(const B & b)36 B(const B& b) : A(b) {} 37 }; 38 main()39int main() 40 { 41 { 42 try 43 { 44 A a(3); 45 std::rethrow_if_nested(a); 46 assert(true); 47 } 48 catch (...) 49 { 50 assert(false); 51 } 52 } 53 { 54 try 55 { 56 throw B(5); 57 } 58 catch (const B& b) 59 { 60 try 61 { 62 throw b; 63 } 64 catch (const A& a) 65 { 66 try 67 { 68 std::rethrow_if_nested(a); 69 assert(false); 70 } 71 catch (const B& b) 72 { 73 assert(b == B(5)); 74 } 75 } 76 } 77 } 78 { 79 try 80 { 81 std::rethrow_if_nested(1); 82 assert(true); 83 } 84 catch (...) 85 { 86 assert(false); 87 } 88 } 89 } 90