Throwable class design
Andrej Mitrovic
andrej.mitrovich at gmail.com
Thu Jan 31 17:36:13 PST 2013
On 2/1/13, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> Self pro-tip: Wrap it in a template
Here, and I didn't even have to use string mixins:
import std.format;
template Wrap(alias func, Exceptions...)
{
auto ref Wrap(string file = __FILE__, size_t line = __LINE__,
Args...)(Args args)
{
foreach (Ex; Exceptions)
{
try
{
return func(args);
}
catch (Ex exc)
{
exc.file = file;
exc.line = line;
throw exc;
}
}
assert(0); // silence compiler
}
}
void foo(int x)
{
if (x)
throw new Exception("");
else
throw new FormatException("");
}
void main()
{
foo(0); // L37: std.format.FormatException at test.d(32):
foo(1); // L38: object.Exception at test.d(30):
alias Wrap!(foo, FormatException, Exception) myFoo;
myFoo(0); // L41: std.format.FormatException at test.d(41):
myFoo(1); // L42: object.Exception at test.d(42):
}
More information about the Digitalmars-d
mailing list