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?