Codes
module test;
mixin template MyExceptionConstructors(bool isBase = false)
{
this(uint errorCode, string errorMessage,
Throwable next = null,
string file = __FILE__, size_t line = __LINE__)
{
static if (isBase)
{
super(errorMessage, file, line, next);
this.errorCode = errorCode;
}
else
super(errorCode, errorMessage, next, file, line);
}
}
class MyException : Exception
{
public:
mixin MyExceptionConstructors!true;
uint errorCode;
}
class WorkException : MyException
{
public:
mixin MyExceptionConstructors!false;
}
struct FailedStatus
{
uint[] codes;
}
class Failed1Exception : MyException
{
public:
mixin MyExceptionConstructors!false;
// Additional constructor cause mixin one be hidden
this(FailedStatus status,
Throwable next = null,
string file = __FILE__, size_t line = __LINE__)
{
this(status.codes[0], "test2");
this.status = status;
}
FailedStatus status;
}
void main()
{
// ok
throw new WorkException(1, "work");
// failed case 1
throw new Failed1Exception(1, "test1");
// failed case 2
auto status = FailedStatus([1, 2, 3]);
throw new Failed1Exception(status);
}
Nightly output
onlineapp.d(47): Error: constructor `test.Failed1Exception.this(FailedStatus status, Throwable next = null, string file = __FILE__, ulong line = cast(ulong)__LINE__)` is not callable using argument types `(uint, string)`
this(status.codes[0], "test2");
^
onlineapp.d(47): Note: constructor `test.Failed1Exception.this` hides base class constructor `test.MyException.MyExceptionConstructors!true.this`
onlineapp.d(60): Error: constructor `test.Failed1Exception.this(FailedStatus status, Throwable next = null, string file = __FILE__, ulong line = cast(ulong)__LINE__)` is not callable using argument types `(int, string)`
throw new Failed1Exception(1, "test1");
^
onlineapp.d(60): Note: constructor `test.Failed1Exception.this` hides base class constructor `test.MyException.MyExceptionConstructors!true.this`
Codes
Nightly output