When running this, the unit tests for Custom Exceptions do not
pass.
Is the mistake on my side or D's side?
Using DMD version 2.112.0 on Fedora Linux 43.
```
import std.stdio : writeln;
import std.exception : assertThrown, assertNotThrown;
void main()
{
}
class UnequalLengths : Exception
{
this(string msg, string file = __FILE__, size_t line = __LINE__)
{
super(msg, file, line);
writeln("Unequal lengths");
}
}
class RangeError : Exception
{
this(string msg, string file = __FILE__, size_t line = __LINE__)
{
super(msg, file, line);
writeln("Range error");
}
}
void average(int[] a, int[] b)
{
throw new UnequalLengths("Unequal lengths");
}
unittest
{
// Must throw UnequalLengths for uneven slices
assertThrown!UnequalLengths(average([1], [1, 2]));
/* The equivalent of the line above */
/+
{
auto isUnequalLengthsThrown = false;
try
{
average([1], [1, 2]);
}
catch (UnequalLengths exc)
{
isUnequalLengthsThrown = true;
}
assert(isUnequalLengthsThrown == true);
}
+/
// Must not throw RangeError for empty slices (it may throw
other types of exceptions)
assertNotThrown!RangeError(average([], []));
/* The equivalent of the line above */
/+
{
auto inRangeErrorThrown = false;
try
{
average([1], [1, 2]);
}
catch (RangeError exc)
{
inRangeErrorThrown = true;
}
catch (Exception exc)
{
// do nothing
}
assert(inRangeErrorThrown == false);
}
+/
}
```
Terminal output:
```
bb@fedora:~/temp/c42_p217_6b_testing_for_exceptions_custom_exceptions$ dub test
--force
No source files found in configuration 'library'.
Falling back to default configuration for test runner.
Starting Performing "unittest" build using /usr/bin/dmd for
x86_64.
Building c42_6b_testing_for_exceptions_custom_exceptions
~master: building configuration [application]
Linking c42_6b_testing_for_exceptions_custom_exceptions
Running c42_6b_testing_for_exceptions_custom_exceptions
Unequal lengths
Unequal lengths
app.UnequalLengths@source/app.d(29): Unequal lengths
----------------
source/app.d:29 void app.average(int[], int[]) [0x400d4c]
source/app.d:53 pure @nogc @safe void
app.__unittest_L32_C1().__dgliteral_L53_C36() [0x400e4c]
/usr/include/dmd/phobos/std/exception.d:149 pure @safe void
std.exception.assertNotThrown!(app.RangeError,
void).assertNotThrown(lazy void, immutable(char)[],
immutable(char)[], ulong) [0x402858]
source/app.d:53 void app.__unittest_L32_C1() [0x400daf]
??:? void app.__modtest() [0x402aa4]
??:? int
core.runtime.runModuleUnitTests().__foreachbody_L603_C5(object.ModuleInfo*) [0x40ce8a]
??:? int object.ModuleInfo.opApply(scope int
delegate(object.ModuleInfo*)).__lambda_L2519_C13(immutable(object.ModuleInfo*)) [0x40350b]
??:? int rt.minfo.moduleinfos_apply(scope int
delegate(immutable(object.ModuleInfo*))).__foreachbody_L585_C5(ref rt.sections_elf_shared.DSO) [0x405b4b]
??:? int rt.sections_elf_shared.DSO.opApply(scope int
delegate(ref rt.sections_elf_shared.DSO)) [0x405cd5]
??:? int rt.minfo.moduleinfos_apply(scope int
delegate(immutable(object.ModuleInfo*))) [0x405ad9]
??:? int object.ModuleInfo.opApply(scope int
delegate(object.ModuleInfo*)) [0x4034dd]
??:? runModuleUnitTests [0x40ccbf]
??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int
function(char[][])*).runAll() [0x404a14]
??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int
function(char[][])*).tryExec(scope void delegate()) [0x4049a1]
??:? _d_run_main2 [0x404917]
??:? _d_run_main [0x40471f]
/usr/include/dmd/druntime/import/core/internal/entrypoint.d:29
main [0x400e71]
??:? [0x7fad9b9d15b4]
??:? __libc_start_main [0x7fad9b9d1667]
??:? _start [0x400b64]
1/1 modules FAILED unittests
Error Program exited with code 1
```