C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Exception compatibility with aliens

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 18 Jan 2023 10:07:10 -0500
On Wed, Jan 18, 2023 at 4:45 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> I have sent this email to two mailing lists for programming language
> proposals, the one for C++ and the one for Python. If you reply to
> this email, please make sure you reply to both. You can see the
> mailing list archives for this month for each list here:
>
> C++ : https://lists.isocpp.org/std-proposals/2023/01/date.php
> Python : https://mail.python.org/archives/list/python-dev_at_[hidden]/2023/1/
>
> Both C++ and Python have exception handling, however a C++ program
> which links with a Python library is unable to handle an exception
> thrown from Python.
>
> Is it conceivable in the future that the C++ Standards Committee and
> the Python Steering Council could cooperate and furnish each other
> with an exhaustive list of exceptions thrown from their respective
> standard libraries, so that both languages can implement compatibility
> with each other's exceptions?
>
> If such a list were to accompany each C++ standard and each Python
> Language Reference, then for example the C++ standard library could
> provide classes for handling Python exceptions:

That's not a reasonable thing to have in the standard library. A
Python library can and should do that, but the C++ standard library
should not have features that are so tied to a specific language like
that.

Also, this proposal seems to be missing the biggest issue with
cross-language exception handling: the fact that you can't throw
exceptions across languages. The only thing you *can* do is catch
exceptions on the source language end, convert them into some data
packet, and throw a different exception on the destination language
side.

That is, having some class representing an external exception is not
the biggest problem in having external exceptions. Any C++ binding for
a language can provide such exception types; you can just use the
types the binding system provides. The problem is implementing the
translation layer uniformly. Which this proposal doesn't address.

Now, C++23 does have a generic tool for transmitting foreign
exceptions in a uniform way: `std::expected`. Indeed, it would make a
lot more sense to use that as the interface between the external
language and C++.

Received on 2023-01-18 15:07:29