Creating code for custom exception handling.
However, when I try to define what() in the list_error class, I get the following message:
Virtual function 'what' has a different return type ('std::string' (aka' basic_string <char, char_trits <char>, allocator <char>>') than the function it overrides (which has returned type) *
Does this require any changes to the original configuration?Thank you for your understanding.
#include<string>
# include <exception>
using std::string;
class list_error:public std::exception {
private:
US>string errorMessage;
public:
list_error(string errorMessage=""): errorMessage(errorMessage){}
virtual~list_error(){}
US>string what() const {return this ->errorMessage;}
};
As you can see in the error message, the return value type is different and should be const char*
.
errorMessage
should also be defined in the same type or converted to use (when returning what().
Chapter 5 Exception Handling (C++ Programming Guide)
Using Runtime Functions and Predefined Exceptions
The standard header contains functions related to classes and exceptions specified in the C++ standard.This header is accessible only when compiling (using compiler default mode or option -compat=5) in normal mode.The following declarations are included in the standard header:
// Omitted before and after
virtual const char * what() const row();
Standard class exception is the base class of all exceptions sent by the selected language structure or C++ standard library. Objects of type exception can be built, copied, or destroyed without generating exceptions.The virtual member function what() returns a string describing the exception.
// Class representing exceptions
class some_exception
{
private:
const char*msg;// message describing the exception
public:
some_exception(const char*msg):msg(msg){}// constructor
const char * what() { return msg; } // Return message
};
class Exception:public std::exception
{
std::string_msg;
public:
Exception(conststd::string&msg):_msg(msg){}
virtual const char * what() const noxcept override
{
return_msg.c_str();
}
};
682 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
546 Who developed the "avformat-59.dll" that comes with FFmpeg?
543 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
524 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.