September 27, 2014
On Thursday, 25 September 2014 at 13:58:23 UTC, Andrei Alexandrescu wrote:
> On 9/25/14, 4:55 AM, Kagamin wrote:
>> On Wednesday, 24 September 2014 at 15:07:05 UTC, Andrei Alexandrescu wrote:
>>> I wonder how difficult would be to create a wrapper class CppException
>>> for everything derived from C++ std::exception.
>>
>> Why not catch std::exception directly? Then you could generate code,
>> just like C++ compiler does it.
>
> That would be a language change - right now D can only catch Exception objects.
>
> Andrei

I've implemented an experemental "framework" to catching C++ exceptions from D.
https://github.com/IgorStepanov/D-CPP-Exception-Handle
It works with G++, and depends on "unwind-cxx.h" header, which has been stealed from "libstdc++/libsupc++/". libsupc++ is a part of libstdc++ and this file can be found in libstdc++ sources. However, libstdc++ developers doesn't publish this header and it cannot be found in /usr/include/

This "framework" allows to call external C++ function and call exception, if good handler is passed. Also it can process polymorphic exceptions: in attached example I throws std::logic_exception and catches std::exception:

//C++

#include <stdexcept>

//test function
void throwEx(void *)
{
    throw std::logic_error("Catch me, if you can");
}

//D
extern(C++) void throwEx(void *);

void main()
{
    /*
    code like ...
    try
    {
        throwEx(null);
    }
    catch(std.exception val)
    {
        printf("exception: '%s'\n", val.what());
    }

    may be rewritten as
    */

    Try!(throwEx)(
        (CPPException!int ex)
        {
            printf("exception: '%d'\n", *ex.data);
            return 0;
        },
        (CPPException!(stdexceptions.std.exception) ex)
        {
            printf("exception: '%s'\n", ex.data.what());
            return 0;
        }
    );
}
//prints: exception: 'Catch me, if you can'


However, DMD can't pass ะก++ exceptions through D function (including extern(C++)). Thus, we cant pass delegate to Try function instead of  throwEx, but this trouble can be resolved, I think.
September 27, 2014
Congrat, that is some awesome work. I will certainly dig into this.
September 27, 2014
On 2014-09-23 19:37, Andrei Alexandrescu wrote:
> We need a libunwind expert to figure out a good approach for handling
> exceptions thrown by C++ code into D.

BTW, are you only interested in handling C++ exception, or are you interested in handling D exceptions in C++ as well?

One ugly hack is to register a terminate handler. Then in the handler extract the necessary information from the exception, create a D exception with this information and throw it as a regular D exception.

Throwing a D exception that should be catchable in C++ is a bit more tricky. It's possible to wrap the a call to a D function in a try-catch block. Convert the D exception to a C++ exception, then throw it using a function part of the C++ exception runtime. The problem here is that C++ won't be able to catch this exception because there's no personality function (or similar) setup by the D compiler.

Ideally D should just use the same exception mechanism as C++. I don't think a language change is necessary for this. Changes in the compiler, yes, but hopefully not in the language.

-- 
/Jacob Carlborg
September 27, 2014
On Saturday, 27 September 2014 at 09:53:37 UTC, Jacob Carlborg wrote:
> On 2014-09-23 19:37, Andrei Alexandrescu wrote:
>> We need a libunwind expert to figure out a good approach for handling
>> exceptions thrown by C++ code into D.
>
> BTW, are you only interested in handling C++ exception, or are you interested in handling D exceptions in C++ as well?
>

> Ideally D should just use the same exception mechanism as C++. I don't think a language change is necessary for this. Changes in the compiler, yes, but hopefully not in the language.

C++ exception mechanism uses C++ type_info objects. We can inherit object.Throwable from std::exception (through extern(C++) interface), override the what() method, but there are no way to generate C++ type_info for D class now. If we want to do a compiler support of C++ exceptions, we should implement and support another one non-standartized feature: type_info. BTW it allows to do dynamic_cast over C++ classes in D, but I think, nobody approve this suggestion, because it can be hard).
September 27, 2014
On Saturday, 27 September 2014 at 11:34:32 UTC, IgorStepanov wrote:

> C++ exception mechanism uses C++ type_info objects. We can inherit object.Throwable from std::exception (through extern(C++) interface), override the what() method, but there are no way to generate C++ type_info for D class now. If we want to do a compiler support of C++ exceptions, we should implement and support another one non-standartized feature: type_info. BTW it allows to do dynamic_cast over C++ classes in D, but I think, nobody approve this suggestion, because it can be hard).

Objective-C can do it somehow. If they can do it I'm sure we can as well.

--
/Jacob Carlborg
September 27, 2014
On 9/27/14, 2:53 AM, Jacob Carlborg wrote:
> On 2014-09-23 19:37, Andrei Alexandrescu wrote:
>> We need a libunwind expert to figure out a good approach for handling
>> exceptions thrown by C++ code into D.
>
> BTW, are you only interested in handling C++ exception, or are you
> interested in handling D exceptions in C++ as well?

The more the merrier, but there's a large difference in importance. For the most part D code (new) will call into C++ code (old) so it's the C++ exceptions we're most worried about. -- Andrei
September 27, 2014
On Saturday, 27 September 2014 at 18:33:24 UTC, Jacob Carlborg wrote:
> On Saturday, 27 September 2014 at 11:34:32 UTC, IgorStepanov wrote:
>
>> C++ exception mechanism uses C++ type_info objects. We can inherit object.Throwable from std::exception (through extern(C++) interface), override the what() method, but there are no way to generate C++ type_info for D class now. If we want to do a compiler support of C++ exceptions, we should implement and support another one non-standartized feature: type_info. BTW it allows to do dynamic_cast over C++ classes in D, but I think, nobody approve this suggestion, because it can be hard).
>
> Objective-C can do it somehow. If they can do it I'm sure we can as well.
>
> --
> /Jacob Carlborg

If someone from D commanders bless me, I can start to exploring and implementing std::type_info for D classes. If we made it, we will implement 50% of C++ exception handling.
September 27, 2014
On Saturday, 27 September 2014 at 19:16:24 UTC, Andrei Alexandrescu wrote:
> On 9/27/14, 2:53 AM, Jacob Carlborg wrote:
>> On 2014-09-23 19:37, Andrei Alexandrescu wrote:
>>> We need a libunwind expert to figure out a good approach for handling
>>> exceptions thrown by C++ code into D.
>>
>> BTW, are you only interested in handling C++ exception, or are you
>> interested in handling D exceptions in C++ as well?
>
> The more the merrier, but there's a large difference in importance. For the most part D code (new) will call into C++ code (old) so it's the C++ exceptions we're most worried about. -- Andrei

Can someone implement an C++ exception transparency? (when exception, throwed from C++ can be passed through D function and landed down in C++ code).
Then, if type_info is implemented, I will able to try to implement C++ exception catching.
September 27, 2014
On 27 September 2014 10:53, Jacob Carlborg via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> On 2014-09-23 19:37, Andrei Alexandrescu wrote:
>>
>> We need a libunwind expert to figure out a good approach for handling exceptions thrown by C++ code into D.
>
>
> BTW, are you only interested in handling C++ exception, or are you interested in handling D exceptions in C++ as well?
>
> One ugly hack is to register a terminate handler. Then in the handler extract the necessary information from the exception, create a D exception with this information and throw it as a regular D exception.
>
> Throwing a D exception that should be catchable in C++ is a bit more tricky. It's possible to wrap the a call to a D function in a try-catch block. Convert the D exception to a C++ exception, then throw it using a function part of the C++ exception runtime. The problem here is that C++ won't be able to catch this exception because there's no personality function (or similar) setup by the D compiler.
>
> Ideally D should just use the same exception mechanism as C++. I don't think a language change is necessary for this. Changes in the compiler, yes, but hopefully not in the language.
>

Well, ObjC++ shares the same EH personality routines as C++, which probably accounts for the compatibility. :)

Iain.
September 27, 2014
On 9/27/14, 12:53 PM, IgorStepanov wrote:
> On Saturday, 27 September 2014 at 18:33:24 UTC, Jacob Carlborg wrote:
>> On Saturday, 27 September 2014 at 11:34:32 UTC, IgorStepanov wrote:
>>
>>> C++ exception mechanism uses C++ type_info objects. We can inherit
>>> object.Throwable from std::exception (through extern(C++) interface),
>>> override the what() method, but there are no way to generate C++
>>> type_info for D class now. If we want to do a compiler support of C++
>>> exceptions, we should implement and support another one
>>> non-standartized feature: type_info. BTW it allows to do dynamic_cast
>>> over C++ classes in D, but I think, nobody approve this suggestion,
>>> because it can be hard).
>>
>> Objective-C can do it somehow. If they can do it I'm sure we can as well.
>>
>> --
>> /Jacob Carlborg
>
> If someone from D commanders bless me, I can start to exploring and
> implementing std::type_info for D classes. If we made it, we will
> implement 50% of C++ exception handling.

Would that be for throwing exceptions from D into C++? I think we can postpone that for now. -- Andrei