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.

Reply via email to