• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--------------------- inherited_exception.cpp ------------------------===//
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 #include <assert.h>
11 
12 struct Base {
13   int b1;
14 };
15 
16 struct Base2 {
17   int b2;
18 };
19 
20 struct Child : public Base, public Base2 {
21   int c;
22 };
23 
f1()24 void f1() {
25   Child child;
26   child.b1 = 10;
27   child.b2 = 11;
28   child.c = 12;
29   throw child;
30 }
31 
f2()32 void f2() {
33   Child child;
34   child.b1 = 10;
35   child.b2 = 11;
36   child.c = 12;
37   throw static_cast<Base2&>(child);
38 }
39 
f3()40 void f3() {
41   Child* child  = new Child;
42   child->b1 = 10;
43   child->b2 = 11;
44   child->c = 12;
45   throw static_cast<Base2*>(child);
46 }
47 
main()48 int main()
49 {
50     try
51     {
52         f1();
53         assert(false);
54     }
55     catch (const Child& c)
56     {
57         assert(true);
58     }
59     catch (const Base& b)
60     {
61         assert(false);
62     }
63     catch (...)
64     {
65         assert(false);
66     }
67 
68     try
69     {
70         f1();
71         assert(false);
72     }
73     catch (const Base& c)
74     {
75         assert(true);
76     }
77     catch (const Child& b)
78     {
79         assert(false);
80     }
81     catch (...)
82     {
83         assert(false);
84     }
85 
86     try
87     {
88         f1();
89         assert(false);
90     }
91     catch (const Base2& c)
92     {
93         assert(true);
94     }
95     catch (const Child& b)
96     {
97         assert(false);
98     }
99     catch (...)
100     {
101         assert(false);
102     }
103 
104     try
105     {
106         f2();
107         assert(false);
108     }
109     catch (const Child& c)
110     {
111         assert(false);
112     }
113     catch (const Base& b)
114     {
115         assert(false);
116     }
117     catch (const Base2& b)
118     {
119         assert(true);
120     }
121     catch (...)
122     {
123         assert(false);
124     }
125 
126     try
127     {
128         f3();
129         assert(false);
130     }
131     catch (const Base* c)
132     {
133         assert(false);
134     }
135     catch (const Child* b)
136     {
137         assert(false);
138     }
139     catch (const Base2* c)
140     {
141         assert(true);
142     }
143     catch (...)
144     {
145         assert(false);
146     }
147 }
148