0

I have a c++14 exception class, a base exception that inherits from std::exception. From the base class I have created derived exceptions, two of them are shown in the code below (BadRequest and ServerError).

this is my exception class, following code:

#ifndef CODEDATABASE_MYEXCEPTIONS_HPP
#define CODEDATABASE_MYEXCEPTIONS_HPP

#include <string>
#include <utility>
using namespace std;

class MyException : public exception {
protected:
public:
    MyException(string error, uint32_t code, string type) :
    error_(move(error)), code_(code),type_(move(type)) {}

public:
    const std::string &getError() const {
        return error_;
    }

    uint32_t getCode() const {
        return code_;
    }

    const std::string &getType() const {
        return type_;
    }

private:
    string error_;
    uint32_t code_;
    string type_;

};

class BadRequest : public MyException {
public:
    explicit BadRequest(const string& message):
        MyException("BadRequest error: " + message, 400, "BadRequest") {

    }
};

class ServerError : public MyException {
public:
    explicit ServerError(const string& message):
            MyException("Server error: " + message, 500, "ServerError") {

    }
};

#endif //CODEDATABASE_MYEXCEPTIONS_HPP

and here is my driver code, to test those exceptions

#include <iostream>
#include "catch.hpp"
#include "exceptions/MyExceptions.hpp"

void internal_raise(bool badrequest) {
    if (badrequest) {
        throw BadRequest("a bad request");
    } else {
        throw ServerError(" a server error");
    }
}

void raise_exception(bool badrequest) {
    try {
        internal_raise(badrequest);
    } catch (MyException& e) {
        throw e; //WARNING IS REPORTED
    }
}

TEST_CASE("work on exceptions", "[exceptions]") {

    try {
        raise_exception(true);
    } catch (MyException& e) {
        std::cout << e.getError() << std::endl;
    }

    try {
        raise_exception(false);
    } catch (MyException& e) {
        std::cout << e.getError() << std::endl;
    }
}

The code works, however I am taking a warning that I don't understand and I will like to know how to fix it

Thrown exception type is not nothrow copy constructible.

This is reported in method raise_exception at the "throw e" statement. Why I am getting this working? What is the proper way to avoid this warning?

Captain Nemo
  • 345
  • 2
  • 14
  • 1
    See [this](https://stackoverflow.com/questions/46697861/handling-thrown-exception-type-is-not-nothrow-copy-constructible-warning) and [this](https://wiki.sei.cmu.edu/confluence/display/cplusplus/ERR60-CPP.+Exception+objects+must+be+nothrow+copy+constructible). – kakkoko Nov 01 '19 at 09:04
  • 2
    **Never** do `using namespace std;` into the global namespace in a header file. – Ted Lyngmo Nov 01 '19 at 09:08
  • @TedLyngmo, true, just removed the std:: prefix here for code readability. – Captain Nemo Nov 01 '19 at 10:30
  • 1
    For most people, I dare to say, it becomes less readable because one has to start checking which `` that is really used etc. Especially when you mix `string` and `std::string`. You also risk introducing unwanted behaviour in your minimal example that wasn't even in your original code that made you ask a question here. – Ted Lyngmo Nov 01 '19 at 11:31

0 Answers0