I'm trying to write an error that derives from runtime_error.
Here's the definition:
class read_error final: public std::runtime_error {
public:
enum class Code;
read_error(Code code);
const char* what() const throw();
Code getCode() const throw();
private:
Code code;
};
And I'm trying to call the constructor like so:
read_error::read_error(Code code): code(code) {
std::string _what;
switch (code) {
case Code::TIME_OUT:
_what = "Read exception: Reading timed out";
case Code::DESERIALIZE:
_what = "Read exception: Something went wrong while deserializing the object";
case Code::CHECKSUM:
_what = "Read exception: Mismatched checksum";
case Code::NO_START_BYTE:
_what = "Read exception: Malformed message, no start byte at front";
case Code::NO_END_BYTE:
_what = "Read exception: Malformed message, no end byte at back";
runtime_error(_what);
}
}
But that doesn't work. It seems that I have to use the initializer like
read_error::read_error(Code code): code(code), runtime_error(/*something*/) {}
But I can't put a switch statement in there. How do I work around this? I know that I have to initialize it, but I can't edit the what_arg inside the runtime_error after the initialization. How do I initialize it using that switch statement?
(I know that there's something still wrong with the enum class definition, I'm working on that too)
Aucun commentaire:
Enregistrer un commentaire