1) The default constructor is not needed , at least you have the code now, so you can delete
MyException ();
2) It is recommended to throw exceptions from std::exception
.
3) You can catch your exceptions by catching MyException&
and typing a message there:
try { PersonValidator validator(Person()); } catch(const MyException& ex) { std::cout << ex.getMessage(); }
4) Avoid using
directives in headers. . Your syntax tells you there is using namespace std;
. This is wrong, you must approve the full name, at least in the headers:
protected: std::string message; MyException (std::string mes)
and etc.
5) Use pass by const reference instead of passing by value for complex types:
MyException (const std::string& mes) PersonValidator (const Person& p)
6) The goal for constant correctness :
std::string getMessage()
it should be:
std::string getMessage() const
since he does not change any members.
7) Use initialization lists :
MyException (string mes) { this->message = mes; }
becomes
MyException (string mes) : message(mes) { }
source share