xfail gnat.dg/trampoline3.adb scan-assembler-not check on hppa*-*-*
[official-gcc.git] / libphobos / testsuite / libphobos.exceptions / chain.d
blob0305707fc62aea6cad941b58a1b9e5ba9b5f0af3
1 // Author: Ali Çehreli
2 // See https://forum.dlang.org/post/o2n7f8$2p1t$1@digitalmars.com
4 import core.stdc.stdio;
6 class TestException : Exception
8 this(string msg)
10 super(typeof(this).stringof~": "~msg);
14 class TestError : Error
16 this(string msg)
18 super(typeof(this).stringof~": "~msg);
22 // Causes an exception chain where the node at index errorIndex is an
23 // Error (others are all Exceptions).
24 void causeExceptionChain(size_t chainLength, size_t errorIndex)
26 void throws(size_t n)
28 scope (exit)
30 string msg = [ cast(char)('a'+n) ].idup;
31 if (n == errorIndex)
33 throw new TestError(msg);
35 else
37 throw new TestException(msg);
41 if (n != 0)
43 // Redundant 'return' keyword due to
44 // https://issues.dlang.org/show_bug.cgi?id=16960
45 return throws(n - 1);
49 throws(chainLength - 1);
52 void main()
54 try
56 // -1 would mean "no Error in the chain". Change this to a
57 // number between 0 and 4 (inclusive) then you will realize
58 // that the Exception below will not be caught.
59 size_t errorIndex = 3;
60 causeExceptionChain(5, errorIndex);
62 catch (Error original)
64 printf("Caught\n");
65 string prefix = "";
66 for ({ size_t i; Throwable ex = original; } ex; ex = ex.next, ++i)
68 printf("%.*s%.*s\n", cast(int)prefix.length, prefix.ptr, cast(int)ex.msg.length, ex.msg.ptr);
69 prefix = prefix~" ";
71 printf("Bypassed chain was:\n");
72 prefix = "";
73 for ({ size_t i; Throwable ex = original.bypassedException; } ex; ex = ex.next, ++i)
75 printf("%.*s%.*s\n", cast(int)prefix.length, prefix.ptr, cast(int)ex.msg.length, ex.msg.ptr);
76 prefix = prefix~" ";