Drawbacks of exceptions being globally allocated

Tejas notrealemail at gmail.com
Sat Aug 14 13:24:22 UTC 2021


On Saturday, 14 August 2021 at 11:41:36 UTC, Tejas wrote:
> What is the drawback of the following "simple" ```@nogc``` 
> exception creation technique?
>
> ```d
> import std;
> void main()@nogc
> {
>     try{
>         __gshared a = new Exception("help");
>         scope b = a;
>         throw b;
>     }
>     catch(Exception e){
>         printf("caught");
>     }
> }
> ```

I mean, there has to be a gotcha, some weird multi-threading case 
where this catastrophically breaks and is therefore not 
recommended, right? Otherwise, why can't we just use this instead 
of fretting with ```DIP 1008```? I even tried accessing the 
actual object in the code below, it didn't crash the program :D

```d
import std;
void main()@nogc
{
     try{
         __gshared a = new Exception("help");
         scope b = a;
         throw b;
     }
     catch(Exception e){
         printf("caught\n");
         printf(cast(char*)e.msg);//new code
     }
}
```


More information about the Digitalmars-d-learn mailing list