1

I've got one project that is compiled to a library (static .lib or dynamic .dll), which is compiled with C++ exceptions enabled. Then I've got another, separate project, that I have no control over, where exceptions are disabled, and that project links to the library that has exceptions enabled (and obviously uses classes and functions from the library that it's linking in).

To clarify:

  • Library (static or dynamic, let's say .lib file): Compiled with exceptions enabled (C++ compiler flag). The library can throw but can catch all its exceptions so I can make it so that it would not leak any exceptions to the "outside"
  • Project: It has exceptions disabled via compiler flag, and I cannot change this. This project links against the library above.

Is this a supported scenario or does that result in undefined behavior or something like that? Let's assume that inside the .lib, I catch all exceptions that could possibly occur and handle them, and no exceptions are "leaked" or thrown out of the .lib back into the project that does not have exceptions enabled.

Does it depend on the platform (Win/Linux/macOS etc.) or compiler (MSVC/clang/gcc) whether this works? Does it depend on whether the library is created and linked as static or dynamic library?

If it matters, the projects are compiled in C++14 mode.

This question is somewhat related, it basically says "Don't allow exceptions to propagate between two pieces of code" (unless they have the exact same compiler flags). But it doesn't specifically answer my question, which is, given I catch all exceptions (so none should cross boundaries, in theory), to link together an exception-enabled library to an exception-disabled project.

Ela782
  • 5,041
  • 5
  • 53
  • 66
  • Exceptions most not crossing into (be unwound into) code that does not have exceptions enabled and/or does not understand exceptions.Some code throws, stack unwinding starts, a stackframe is encountered that as not setup for exceptions bad things happen. – Richard Critten Aug 08 '18 at 20:18
  • @RichardCritten I am aware of that. As I wrote in the question, I catch all the exceptions in the library that is compiled with exceptions enabled. So in theory, I guess no exceptions should ever cross the "border". But I am still linking non-exception enabled code to exception-enabled code and asking whether this is defined or undefined behavior. – Ela782 Aug 08 '18 at 20:20
  • I am confused. Are you linking a no exception library into your exception project? Or the other way round? Also how do you mean it disabled? All functions are marked as nothrow? Or is it an option in your IDE? Is the library precompiled? – Volt Aug 08 '18 at 21:53
  • @Volt I am linking an exception-enabled library (i.e. exceptions enabled via compiler flag, the library has throw statements, etc.) into a project that has exceptions disabled via compiler flags. As mentioned, I do have (partial) control over the exception-enabled library, so I can catch all exceptions and hopefully make it so that it would never "leak" an exception back to the exception-disabled project. – Ela782 Aug 08 '18 at 21:56

0 Answers0