Add test for the last chapter of our C++ exception handling odyssey. llvmg++
[oota-llvm.git] / test / C++Frontend / EH / function_try_block.cpp
1
2 #include <stdio.h>
3
4 static unsigned NumAs = 0;
5
6 struct A {
7   unsigned ANum;
8   A() : ANum(NumAs++) { printf("Created A #%d\n", ANum); }
9   A(const A &a) : ANum(NumAs++) { printf("Copy Created A #%d\n", ANum); }
10   ~A() { printf("Destroyed A #%d\n", ANum); }
11 };
12
13 static bool ShouldThrow = false;
14
15 int throws()
16   try
17 {
18   if (ShouldThrow) throw 7; return 123;
19 } catch (...) {
20   printf("'throws' threw an exception: rethrowing!\n");
21   throw;
22 }
23
24 struct B {
25   A a0, a1, a2;
26   int i;
27   A a3, a4;
28
29   B();
30   ~B() { printf("B destructor!\n"); }
31 };
32
33 B::B()
34 try 
35 : i(throws())
36 {
37   printf("In B constructor!\n");
38 }
39 catch (int i) {
40   printf("In B catch block with int %d: auto rethrowing\n", i);
41 }
42
43
44 int main() {
45   {
46     B b;   // Shouldn't throw.
47   }
48
49   try {
50     ShouldThrow = true;
51     B b;
52   } catch (...) {
53     printf("Caught exception!\n");
54   }
55 }