# Embedded cpp exceptions

**URL:** <https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400>\
**Category:** Help!\
**Created:** [November 24, 2025, 9:41pm UTC](https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400 "2025-11-24T21:41:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgondan1](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mgondan1/32/803_2.png) [@mgondan1](https://swi-prolog.discourse.group/u/mgondan1)\
**Post date:** [November 24, 2025, 9:41pm UTC](https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400/1 "2025-11-24T21:41:32Z")

</div>

That is the example from @peter.ludemann’s cpp2 pack, I used the code from [SWI-Prolog -- The class PlTerm\_tail](https://www.swi-prolog.org/pldoc/man?section=cpp2-pltail) and added a typo that should raise an exception.

```prolog
int
main(int argc, char *argv[])
{ PlEngine e(argv[0]);
  PlTermv av(1);
  PlTerm_tail l(av[0]);

  for(int i=0; i<argc; i++)
    PlCheckFail(l.append(PlTerm_string(argv[i])));
  PlCheckFail(l.close());

  PlQuery q("writelnnnn", av); // Note the typo
  try
  { return q.next_solution() ? 0 : 1;
  } catch ( PlException &ex )
  { std::cerr << ex.what() << std::endl;
    return 1;
  }
}

```

If I compile it using swipl-ld example.cpp and then ./a.out 1 2 3, I don’t get an exception.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [November 24, 2025, 10:02pm UTC](https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400/2 "2025-11-24T22:02:17Z")

</div>

I’ll try this out and either tell you why it’s Working As Designed(\*) or I’ll fix the bug/documentation. I’m in the middle of something else, so unless this is urgent, I’ll probably take a few days.

(\*) that is: “Broken As Designed” or “BAD”

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 5, 2025, 5:47am UTC](https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400/3 "2025-12-05T05:47:15Z")

</div>

Sorry I took so long to respond.  
`PlQuery::next_solution()` doesn’t throw an exception – because, as documented, you must use `PL_exception(0)` to determine whether a `false` result means failure or an exception. Alternatively, you can replace `q.next_solution()` with `PlWrap<int>(q.next_solution())` to do the check and throw an exception if warranted.  
The sample code in `main.cpp` uses `PlWrap<int>(...)` but I failed to update the documentation. I’ll do a quick fix to the documentation; I may not be able to properly fix the documentation before the end of the month.

This isn’t an ideal API but it’s necessitated by the possibility of the flag `PL_Q_EXT_STATUS` to `PL_open_query()` which gives results other than `false` or `true` (`0` or `1` )and `PlQuery::next_solution()` doesn’t know that which flag was used. Perhaps this should change … I’ll review a PR if you make one (`pl2cpp.doc` needs to be updated also).

---

<div class="post-metadata">

**Author:** ![mgondan1](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mgondan1/32/803_2.png) [@mgondan1](https://swi-prolog.discourse.group/u/mgondan1)\
**Post date:** [December 5, 2025, 7:53am UTC](https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400/4 "2025-12-05T07:53:13Z")

</div>

I actually switched to PL\_Q\_EXT\_STATUS, as you suggest, and check PL\_exception(0) if an exception occurred. Thanks!

It may take a bit of time until the PR arrives.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [December 5, 2025, 8:02am UTC](https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400/5 "2025-12-05T08:02:14Z")

</div>

> [@peter.ludemann](#):
>
> This isn’t an ideal API but it’s necessitated by the possibility of the flag `PL_Q_EXT_STATUS` to `PL_open_query()` which gives results other than `false` or `true` (`0` or `1` )and `PlQuery::next_solution()` doesn’t know that which flag was used. Perhaps this should change

I don’t know whether it would help, but the flags to create the query are stored, so we could add some call to retrieve them.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 5, 2025, 10:32pm UTC](https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400/6 "2025-12-05T22:32:30Z")

</div>

> [@jan](#):
>
> the flags to create the query are stored, so we could add some call to retrieve them.

I thought about this (the constructor for `PlQuery` could make a copy of the flags if they aren’t available via a PL\_\*() function) but that would give different semantics for `PlQuery::next_solution()`, depending on the flags (that is, it would throw an exception unless the flags are `PL_Q_EXT_STATUS`).

Perhaps a better solution would be to have 2 versions of `PlQuery::next_solution()`, (e.g., `next_solution_check()`, `next_solution_ext`) and check at runtime that the flag is as expected. `PlCheckFail()` is the standard way to throw an exception if `next_solution()` fails.

A function for getting the flag would be a good idea, for debugging if nothing else.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [December 6, 2025, 9:22am UTC](https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400/7 "2025-12-06T09:22:11Z")

</div>

On the other hand, why no go the way it also handled in the JavaScript and Python interfaces: the high level calls PL\_open\_query() with the proper flags that allows us to use PL\_next\_solution() such that it fits the language? Ok, someone can call the low level C PL\_open\_query() with different flags and then hope the high level works, which may not be the case. I think that can simply be documented. Either use the C API all the way to manage the lifetime of a query or use the C++. Don’t mix them.

I’m ok with an API to extract the flag, but only if it really improves the interface. Otherwise it merely bloats the API. For now, I’m not convinced.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 7, 2025, 8:15pm UTC](https://swi-prolog.discourse.group/t/embedded-cpp-exceptions/9400/8 "2025-12-07T20:15:13Z")

</div>

I think that the solution is for `PlQuery::next_solution()` to throw a C++ exception if there was a Prolog exception, otherwise return `false` for failure (and `true` for success) _unless_ the `PlQuery` was constructed with flag `PL_Q_EXT_STATUS`, in which case no C++ exception is thrown and other return values are possible. I don’t see an easy way of enforcing the distinction between the two possible results, either using type declarations or checking at run-time, so we’ll just have to rely on documentation.

AFAIK, it doesn’t hurt to call `PL_exception(0)` twice, so `PlWrap<int>(q.next_solution())` doesn’t hurt even if `PlQuery::next_solution()` checks for an exception, except for the overhead. So, we can leave existing code as-is (except for the examples) and document the situation.

In the case of `PL_Q_EXT_STATUS`, `PlWrap<int>(...)` will neverdetect a Prolog exception because the return code is `-1` (`PL_S_EXCEPTION`) and it won’t call `PL_exception()`; that is, there’s no ambiguity between failure and a Prolog exception.

There’s no need to use the `PL_open_query()` flags when checking the result of `PL_next_solution()` – in the case of `PL_Q_EXT_STATUS`, a zero will always mean failure, so the check for `PL_exception(0)` will always fail. This is a tiny bit of overhead, so probably not worth trying to optimize. (But we could have a variant of `PlQuery::next_solution()` if this bothers anyone)

(Maybe `PlWrap<...>()` should be called `PlPrologExceptionToCppException()`.)
