aboutsummaryrefslogtreecommitdiffstats
path: root/test/C++Frontend/EH/function_try_block.cpp
blob: 001b7c50b821843201c7147c4d8a9cd67b74c884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <stdio.h>

static unsigned NumAs = 0;

struct A {
  unsigned ANum;
  A() : ANum(NumAs++) { printf("Created A #%d\n", ANum); }
  A(const A &a) : ANum(NumAs++) { printf("Copy Created A #%d\n", ANum); }
  ~A() { printf("Destroyed A #%d\n", ANum); }
};

static bool ShouldThrow = false;

int throws()
  try
{
  if (ShouldThrow) throw 7; return 123;
} catch (...) {
  printf("'throws' threw an exception: rethrowing!\n");
  throw;
}

struct B {
  A a0, a1, a2;
  int i;
  A a3, a4;

  B();
  ~B() { printf("B destructor!\n"); }
};

B::B()
try 
: i(throws())
{
  printf("In B constructor!\n");
}
catch (int i) {
  printf("In B catch block with int %d: auto rethrowing\n", i);
}


int main() {
  {
    B b;   // Shouldn't throw.
  }

  try {
    ShouldThrow = true;
    B b;
  } catch (...) {
    printf("Caught exception!\n");
  }
}