# Cpp2 exceptions

**URL:** <https://swi-prolog.discourse.group/t/cpp2-exceptions/6040>\
**Category:** General\
**Created:** [December 20, 2022, 1:06pm UTC](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040 "2022-12-20T13:06:06Z")\
**Posts on this page:** 8\
**Page:** 5

<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 27, 2025, 5:51pm UTC](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/85 "2025-12-27T17:51:18Z")

</div>

As far as I understand the unit tests, it is expected that PL\_get\_nchars fails in some of them (and raises a representation error).

```prolog
test(hello, error(representation_error(encoding))) :-
    hello("世界", _Out).

```

---

<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 29, 2025, 7:08am UTC](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/86 "2025-12-29T07:08:01Z")

</div>

Yes, that test is designed to get an error. because “世界” can’t be represented in ASCII.

Except, I can’t reproduce the error (see [clang.asan: unit tests fail · Issue #102 · SWI-Prolog/packages-cpp · GitHub](https://github.com/SWI-Prolog/packages-cpp/issues/102)) but instead shows up in `clib:crypt` for gcc-12, clang-14, but not clang-19. (I used `ctest j=1` to ensure the tests were isolated from each other, although that probably wasn’t necessary). `PlTerm::get_nchars()` uses `PlEx_fail()` to check whether the “false” return code from `PL_get_nchars()` is simply a failure or is an error (from reading the code, it seems that only a representation error is possible, but there might be other error possibilities).

---

<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 29, 2025, 8:54pm UTC](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/87 "2025-12-29T20:54:42Z")

</div>

Unsure if this helps (maybe on the longer run), but I can reproduce this problem also in a minimalistic example:

example.cpp:

```prolog
#include <SWI-cpp2.h>

PREDICATE(helloex, 0)
{ throw PlUnknownError("an exception");
}

```

Compile with `src/swipl-ld -g -shared example.cpp -o example.so`, then  
`use_foreign_library(example). helloex.`

(cmake configuration as above)

```prolog
CC="clang-21 -fsanitize=address,undefined -fno-sanitize=float-divide-by-zero -fno-sanitize=alignment -fno-sanitize=function -fno-omit-frame-pointer -std=gnu23" CXX="clang++-21 -fsanitize=address,undefined -fno-sanitize=float-divide-by-zero -fno-sanitize=alignment -fno-omit-frame-pointer -frtti -std=gnu++17" CFLAGS="-g -O3 -Wall " LDFLAGS="-Wl,-O1" CXXFLAGS="-g -O3 -Wall -pedantic -fpic" cmake -DCMAKE_CXX_STANDARD=17 -DCMAKE_INSTALL_PREFIX=~/swipl -DMACOSX_DEPENDENCIES_FROM=Homebrew -DSWIPL_INSTALL_IN_LIB=ON -DINSTALL_DOCUMENTATION=OFF -DBUILD_TESTING=ON -DINSTALL_TESTS=ON -DSWIPL_PACKAGES_JAVA=OFF -DSWIPL_PACKAGES_GUI=OFF -DSWIPL_PACKAGES_ODBC=OFF -DSWIPL_PACKAGES_PYTHON=OFF -S ..
make

```

---

<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 30, 2025, 12:15am UTC](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/88 "2025-12-30T00:15:32Z")

</div>

> [@mgondan1](#):
>
> (cmake configuration as above)

Did you set this up with `scripts/configure`? If so, what is the build directory name (with the options)?

I’ll add this trivial test to the `cpp` tests.

---

<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 30, 2025, 8:36am UTC](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/89 "2025-12-30T08:36:40Z")

</div>

No, with the cmake command shown. I’ll try to simplify this during the day, so that it matches the build.make.clang etc logic

---

<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 30, 2025, 2:11pm UTC](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/90 "2025-12-30T14:11:27Z")

</div>

Simpler version:

```prolog
cd swipl-devel
mkdir build.make.clang\=21.asan
cd build.make.clang\=21.asan
../scripts/configure
make
src/swipl-ld -g -shared example.cpp -o example.so
src/swipl
?- use_foreign_library(example).
?- helloex.

```

Needs clang-21, of course.

It can also be reproduced with with gcc (15.2.0 on my computer):

```prolog
cd swipl-devel
mkdir build.make.gcc.asan
cd build.make.gcc.asan
../scripts/configure
make
src/swipl-ld -g -shared example.cpp -o example.so
src/swipl
?- use_foreign_library(example).
?- helloex.

```

---

<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:** [January 3, 2026, 4:36pm UTC](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/91 "2026-01-03T16:36:24Z")

</div>

It seems that I can, well, “fix” the problem with

```prolog
LD_PRELOAD="$(realpath "$(gcc -print-file-name=libasan.so)") $(realpath "$(gcc -print-file-name=libstdc++.so)")" ctest

```

so that it is rather a linker problem. See discussion of a similar problem here. [AddressSanitizer CHECK failed: ../../../sanitizer/asan/asan\_interceptors.cc:384 "((\_\_interception::real\_\_\_cxa\_throw)) when using --as-needed and dynamically loaded .so · Issue #934 · google/sanitizers · GitHub](https://github.com/google/sanitizers/issues/934)

Needless to say that I have no idea what all this stuff is about.

Edit:

```prolog
LD_PRELOAD=$(gcc -print-file-name=libstdc++.so) ctest

```

works, too, if we add -DCMAKE\_BUILD\_TYPE=Sanitize.

---

<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:** [January 3, 2026, 9:05pm UTC](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/92 "2026-01-03T21:05:41Z")

</div>

> [@mgondan1](#):
>
> It seems that I can, well, “fix” the problem with `LD_PRELOAD= ...`

I think I’ve encountered a similar problem in the past that was solved with `LD_PRELOAD` … I forget the details but I think it was something to do with an executable requiring one path for its shared libraries but ASAN requiring a different path; and therefore the runtime ASAN library doesn’t match what the executable was built with. (So, this is something that static linking would probably solve)

I’ll see if I have any notes from this and if there’s a better solution (it was years ago).

Anyway, it appears that the crash is due to an ASAN shared library problem and not due to a bug in either the SWI-cpp API or some subtlety in C++ exceptions.

[Previous page](https://swi-prolog.discourse.group/t/cpp2-exceptions/6040.md?page=4)
