| 1 |
// { dg-options "-fpreview=dip1008" } |
| 2 |
class E : Exception |
| 3 |
{ |
| 4 |
static int instances; |
| 5 |
this(string msg = "", Throwable nextInChain = null) |
| 6 |
{ |
| 7 |
super(msg, nextInChain); |
| 8 |
instances++; |
| 9 |
} |
| 10 |
|
| 11 |
~this() |
| 12 |
{ |
| 13 |
instances--; |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
void main() |
| 18 |
{ |
| 19 |
alias chain = Exception.chainTogether; |
| 20 |
|
| 21 |
assert(chain(null, null) is null); |
| 22 |
|
| 23 |
try |
| 24 |
{ |
| 25 |
throw new E(); |
| 26 |
} |
| 27 |
catch (E e) |
| 28 |
{ |
| 29 |
assert(E.instances == 1); |
| 30 |
assert(e.refcount == 2); |
| 31 |
} |
| 32 |
|
| 33 |
assert(E.instances == 0); |
| 34 |
|
| 35 |
try |
| 36 |
{ |
| 37 |
throw new E(); |
| 38 |
} |
| 39 |
catch (E e) |
| 40 |
{ |
| 41 |
assert(chain(null, e) is e); |
| 42 |
assert(e.refcount == 2); // "Owned by e" + 1 |
| 43 |
} |
| 44 |
|
| 45 |
assert(E.instances == 0); |
| 46 |
|
| 47 |
try |
| 48 |
{ |
| 49 |
throw new E(); |
| 50 |
} |
| 51 |
catch (E e) |
| 52 |
{ |
| 53 |
assert(chain(e, null) is e); |
| 54 |
assert(e.refcount == 2); // "Owned by e" + 1 |
| 55 |
} |
| 56 |
|
| 57 |
assert(E.instances == 0); |
| 58 |
|
| 59 |
try |
| 60 |
{ |
| 61 |
throw new E("first"); |
| 62 |
} |
| 63 |
catch (E first) |
| 64 |
{ |
| 65 |
try |
| 66 |
{ |
| 67 |
throw new E("second"); |
| 68 |
} |
| 69 |
catch (E second) |
| 70 |
{ |
| 71 |
try |
| 72 |
{ |
| 73 |
throw new E("third"); |
| 74 |
} |
| 75 |
catch (E third) |
| 76 |
{ |
| 77 |
assert(chain(first, second) is first); |
| 78 |
assert(first.next is second); |
| 79 |
assert(second.next is null); |
| 80 |
|
| 81 |
assert(chain(first, third) is first); |
| 82 |
assert(first.next is second); |
| 83 |
assert(second.next is third); |
| 84 |
assert(third.next is null); |
| 85 |
|
| 86 |
assert(first.refcount == 2); |
| 87 |
assert(second.refcount == 3); |
| 88 |
assert(third.refcount == 3); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
assert(E.instances == 3); |
| 93 |
} |
| 94 |
|
| 95 |
assert(E.instances == 0); |
| 96 |
|
| 97 |
try |
| 98 |
{ |
| 99 |
throw new E("first"); |
| 100 |
} |
| 101 |
catch (E first) |
| 102 |
{ |
| 103 |
assert(first.refcount == 2); |
| 104 |
assert(E.instances == 1); |
| 105 |
|
| 106 |
try |
| 107 |
{ |
| 108 |
throw new E("second", first); |
| 109 |
} |
| 110 |
catch (E second) |
| 111 |
{ |
| 112 |
assert(first.next is null); |
| 113 |
assert(second.next is first); |
| 114 |
|
| 115 |
assert(first.refcount == 3); |
| 116 |
assert(second.refcount == 2); |
| 117 |
|
| 118 |
assert(E.instances == 2); |
| 119 |
} |
| 120 |
|
| 121 |
assert(first.refcount == 2); |
| 122 |
assert(E.instances == 1); |
| 123 |
} |
| 124 |
|
| 125 |
assert(E.instances == 0); |
| 126 |
} |