Exceptions in @nogc code
Dmitry Olshansky via Digitalmars-d
digitalmars-d at puremagic.com
Sat Apr 1 11:18:13 PDT 2017
On 4/1/17 3:34 PM, Andrei Alexandrescu wrote:
> Walter and I discussed the following promising setup:
>
> Use "throw new scope Exception" from @nogc code. That will cause the
> exception to be allocated in a special stack-like region.
>
> If the catching code uses "catch (scope Exception obj)", then a
> reference to the exception thus created will be passed to catch. At the
> end of the catch block there's no outstanding reference to "obj" so it
> will be freed. All @nogc code must use this form of catch.
>
> If the catching code uses "catch (Exception obj)", the exception is
> cloned on the gc heap and then freed.
>
> Finally, if an exception is thrown with "throw new Exception" it can be
> caught with "catch (scope Exception obj)" by copying the exception from
> the heap into the special region, and then freeing the exception on the
> heap.
>
Horrible. Introducing the verbose way for the most commonly useful
scenario and pushing the complexity on to programmers.
I don't understand what's so difficult to just recognize that "throw new
Exception" creates a unique object that is passed to the exception
handler. So just mark the exception as unique in the exception handler
code so that catch site is aware of it. Are we just bend on penalizing
programmers for not typing out "scope"?
In turn I don't see what adding "scope" to the catch site accomplishes
- you still need to ensure the reference doesn't escape. And if it
doesn't escape you may just as well deallocate the exception object.
> Such a scheme preserves backward compatibility and leverages the work
> done on "scope".
Pardon but I don't see how. It has one good thing going for it - the
same keyword.
---
Dmitry Olshansky
>
> Andrei
More information about the Digitalmars-d
mailing list