I am working on a project that uses a legacy library which uses function definitions like
void func() throw(some_exception);
Since dynamic exception specifications are removed in C++17 I am wondering how to address this problem.
P0003R0 suggests to replace
void func() throw(some_exception)
{
/* body */
}
with something like
void func()
{
try
{
/* body */
}
catch(const some_exception&) {
throw;
}
}
However, I do not have access to the source code (only the header files).
So I am left with trying to "fix" the function definition in the header. So e.g. I could write
void func() noexcept(false);
But when the function throws an exception, my application still terminates.
How can I change the function definition in the header files or possibly adjust my own project (the places where i use func
) to obtain the same behaviour as throw(some_exception)
had before C++17?
The dynamic exception specifications syntax uses the throw keyword to list which exception types a function might directly or indirectly throw:
Just remove the dynamic exception specification. That's all you need to do. C++ is not Java; in C++, all functions are (at a language level) assumed to throw anything unless they are tagged noexcept. As such, if you remove the exception specification, it will work exactly as it did before.
A function with a non-throwing dynamic exception specification does not allow any exceptions. Incomplete types, pointers or references to incomplete types other than cv void*, and rvalue reference types are not allowed in the exception specification.
If a function is declared with type T listed in its dynamic exception specification, the function may throw exceptions of that type or a type derived from it. A dynamic exception speficification without a type-id list (i.e. throw()) is non-throwing.
Just remove the dynamic exception specification. That's all you need to do.
C++ is not Java; in C++, all functions are (at a language level) assumed to throw anything unless they are tagged noexcept
. As such, if you remove the exception specification, it will work exactly as it did before.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With