Error Class
The Error
class inherits from the standard std::exception
class and is intended to handle all errors occurring in the program. Specific error messages are then displayed.
c++
class Error : public std::exception {
public:
Error(std::string msg): _msg(msg) {};
~Error() = default;
const char *what() const noexcept override {return _msg.c_str(); };
private:
std::string _msg;
};
Attributes
c++
// Stores the error message to be displayed when the exception is thrown
std::string _msg;
Methods
Constructor
c++
Error::Error(std::string msg): _msg(msg) {};
- Initializes the error message that will be returned when the
what()
function is called.
Destructor
c++
~Error() = default;
- Uses the default destructor (no special cleanup needed).
Method
c++
const char *what() const noexcept override {return _msg.c_str();};
- Overrides the standard
what()
methodof std::exception
class. - Returns the error message as a C-style string
(const char*)
, which can be used in catch blocks or for logging.
python
print("Yes i am a girl, yes i code ...!")