On 1/20/26 12:42 PM, Brother Bill wrote:
> In a unittest, we have assertThrown average([1], [1, 2]));
> As assert statement throws an exception.
Yes, throws an exception but not Exception.
> But the unittest fails, which seems odd, as an assert did thr
> Have I found another bug in DMD?
I happen to have DMD64 D Compiler v2.109.1 on this environment. Your
unittest passes.
What fails is the code inside 'main'.
> ```
> import std.stdio : writeln;
> import std.exception : enforce, assertThrown, assertNotThrown;
>
> void main()
> {
> auto result = average([], []);
>
> // assert is not caught in catch block
> // enforce is caught in catch block
> try
> {
> result = average([1], [1, 2]);
> }
> catch (Exception e)
> {
> writeln("Caught exception: ", e.msg);
> }
> }
There is the issue: The exception type that assert throws is *not* under
the Exception hierarchy but under the Error hierarchy:
```
Throwable (not recommended to catch)
↗ ↖
Exception Error (not recommended to catch)
↗ ↖ ↗ ↖
... ... ... ...
```
You could catch Throwable or Error. However, as they are not recommended
to be caught, perhaps a better option is to change assert() to
enforce(). enforce() throws a type under the Exception hierarchy.
Ali