On Saturday, 14 August 2021 at 15:28:36 UTC, Alexandru Ermicioi wrote:
On Saturday, 14 August 2021 at 13:24:22 UTC, Tejas wrote:
...

I don't think there are any gotchas here. The problem with this technique, is when your exceptions aren't just simple labels but also carry some additional data, say for example specific error type, and subject that, caused this. In such cases you can't have a gloablly shared instance. Let's say it's doable but has lot's of drawbacks.

Regards,
Alexandru.

Thank you.

If you're willing to help further, would you please tell me why there is a GC allocation in the code below that uses ```emplace```? Will such code truly work if GC is never linked in the program?

```d

import core.lifetime:emplace;
import core.stdc.stdlib:malloc;
int foo(int)@nogc
{
auto mem = cast(Exception)malloc(__traits(classInstanceSize, Exception));
    auto memo = emplace!(Exception,string)(mem, "HOHOH");
    //scope b = a;
    throw memo;
}

void test() @nogc
{
    try
    {
        foo(1);
    }
    catch(Exception e)
    {
    }
}

void main()
{
    import std.stdio;
    import core.memory;
    auto stats1 = GC.stats();
    test();
    auto stats2 = GC.stats();
    writeln(stats1);
    writeln(stats2);
}

Output:
Stats(0, 0, 0)
Stats(1376, 1047200, 1360)

```

Reply via email to