In relation to https://github.com/D-Programming-Language/phobos/pull/3751 I created the following code:
mixin template basicExceptionCtors()
{
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @safe pure nothrow
{
super(msg, file, line, next);
}
this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) @safe pure nothrow
{
super(msg, file, line, next);
}
}
pure nothrow @safe unittest
{
class MeaCulpa: Exception
{
mixin basicExceptionCtors;
}
try
throw new MeaCulpa("test");
catch (MeaCulpa e)
{
assert(e.msg == "test");
assert(e.file == __FILE__);
assert(e.line == __LINE__ - 5);
}
}
void main(){}
Compiling the above I get the error:
<src>(19): Error: <src>.__unittestL12_1.MeaCulpa is thrown but not caught
<src>(12): Error: function '<src>.__unittestL12_1' is nothrow yet may throw
Clearly the thrown exception is caught. As such the compiler incorrectly flags this as a violation of the `nothrow` declaration and should be fixed.
Comment #1 by samjnaa — 2015-12-22T13:20:02Z
I forgot to mention that if the catch block catches an Exception instead of a MeaCulpa, then the compiler does not produce an error w.r.t. `nothrow`.
Also, I'm using latest DMD 2.0.69.2.
Comment #2 by robert.schadek — 2024-12-13T18:46:09Z