Catching C++ Exceptions in D

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed Jan 6 08:53:04 PST 2016


On 01/05/2016 06:56 PM, Walter Bright wrote:
> On 1/5/2016 1:30 PM, Jakob Ovrum wrote:
>> On Tuesday, 5 January 2016 at 17:23:38 UTC, Walter Bright wrote:
>>> At the exit of a catch clause, the destructor on the caught C++
>>> exception will
>>> be run, as would be expected by C++ programmers.
>>
>> Does this work with rethrow? What if a D exception is thrown from a
>> C++ catch
>> block - will the C++ exception still be destroyed properly?
>>
>
> My understanding is that rethrow invokes the copy constructor, so the
> original can still be destroyed.

To clarify what C++ does, consider:

try { ... }
catch (std::exception& e)
{
    throw e;
}

This throws a copy of e and is almost always not the desired/expected 
behavior because the original dynamic type of e (which may be a class 
derived from std::exception) is lost. Now consider:

try { ... }
catch (std::exception& e)
{
    throw;
}

In this case no copy is created. The same exact dynamic object continues 
to be thrown up the chain.


Andrei


More information about the Digitalmars-d mailing list