Including Throwing Exceptions classes to NOEXCEPT project
-
I want to include classes to my noexcept project which throw exceptions (I say about cryptopp).
After that my project can not be built.
How to leave the project noexcept when continue using cryptopp with exceptions?
What technics exist to make class noexcept? (hope there is any besides direct deleating throwing code in original classes) -
Hi @Kofr,
Can you explain what you mean by "can not be built"? Are you saying you get compilation errors? If so, can you show us the errors you get?
I'm not entirely sure what you're asking, but I think the general pattern for using code that may throw exceptions within
noexect
functions is simply to catch all exceptions, and handle them within the function. Something like:void myFunction() noexcept { // don't do anything here that could possibly throw. try { // do something with cryptopp, or other functions that might throw. } catch (const some-specific-exception-type &e) { // handle the exception } catch (const some-other-exception-type &e) { // handle the exception } catch (...) { // handle unknown exceptions } // don't do anything here that could possibly throw. }
Cheers.
-
@Paul-Colby thank you for the answer.
I compile with useqmake
CONFIG += exceptions_off
.
This is same as gcc flag-fno-exceptions
Any use of exceptions cause fail to build. I do not know what is the exact errors.
I think it will not help at all if I catch all exceptions because I do not want to use exceptions mechanism at all. -
From https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html:
If you might have some code that throws, you shouldn't use -fno-exceptions. If you have some code that uses try or catch, you shouldn't use -fno-exceptions.
So the answer is no
-
@Kofr For example of the exterme case, I'm building QtWebKit with disabled exceptions but one file enables them to catch possible std::bad_alloc from Qt:
https://github.com/annulen/webkit/blob/qtwebkit-5.212/Source/WebCore/PlatformQt.cmake#L194
In qmake it's impossible to set flags for one file, so you will need to split exception-enabled code into (static) library
-
@Konstantin-Tokarev I still see the problem. If you build exception throwing code into .lib or .a there is still header which is not noexcept. Is it ok for the compiler to work with such header and lib or the problem is real?
-
@Kofr Seems like you didn't understand problem yet. You need to separate catching code into the library, not only throwing.
So you basically need a wrapper that will call trowing functions and catch all exceptions, then use this wrapper in noexcept code.
-
(I hope you are not going to ignore errors from cryptographic library)
-
@Konstantin-Tokarev said in Including Throwing Exceptions classes to NOEXCEPT project:
sically need a wrapper that will call trowing functions and catch all exceptions, then use this wrapper in noexcept code.
thx, now I got the trick
-
Also, I don't know if this trick works with MSVC, didn't try that yet
-
@Konstantin-Tokarev Visual Studio allows per-file compile flags so the trick should be redundant
-
@VRonin I don't doubt that Visual Studio can do this, my poins are
- qmake doesn't have native way to set source-specific flags, you need to create your own custom target with all gory details of building C++ file, so it's much easier to move it into library
- I don't know if mixing EH flags in the same binary is reliable with MSVC