Processing "Type of exception thrown is not a construction other than the old one." Warning

Return to C ++ development after a 12-year hiatus. I use the JetBrains CLion software, which is great as it provides a big contribution to possible problems in my class. One of the warnings that I get in the instructions to create the class constructor: Thrown exception type is not nothrow copy constructible. Here is an example of the code that generates this warning:

#include <exception>
#include <iostream>

using std::invalid_argument;
using std::string;

class MyClass {
    public:
        explicit MyClass(string value) throw (invalid_argument);
    private:
        string value;
};

MyClass::MyClass(string value) throw (invalid_argument) {
    if (value.length() == 0) {
        throw invalid_argument("YOLO!"); // Warning is here.
    }

    this->value = value;
} 

This piece of code compiles and I can unit test it. But I would really like to get rid of this warning (in order to understand what I'm doing wrong, even if it compiles).

thank

+4
source share
1 answer

, , . ++ 11 throw noexcept. :

explicit MyClass(string value) noexcept(false);

, noexcept(false) , noexcept noexcept(true), :

explicit MyClass(string value);

, "Throw exception type not notrow copy constructible", , .

+1

Source: https://habr.com/ru/post/1687339/


All Articles