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
10 super(typeof(this).stringof
~": "~msg
);
14 class TestError
: Error
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
)
30 string msg
= [ cast(char)('a'+n
) ].idup
;
33 throw new TestError(msg
);
37 throw new TestException(msg
);
43 // Redundant 'return' keyword due to
44 // https://issues.dlang.org/show_bug.cgi?id=16960
49 throws(chainLength
- 1);
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
)
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
);
71 printf("Bypassed chain was:\n");
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
);