1 // { dg-options "-fpreview=dip1008" } 2 class E : Exception 3 { 4 static int instances; 5 this(string msg = "") 6 { 7 super(msg); 8 instances++; 9 } 10 ~this()11 ~this() 12 { 13 instances--; 14 } 15 } 16 main()17void 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