How to replace dynamic exception specifications: throw(...)

Viewed 4806

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.

  1. 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).

  2. 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?

1 Answers

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.

Related