我刚刚创建了异常层次结构,并希望传递char*给我的一个派生类的构造函数,并带有一条告诉错误的消息,但显然std::exception没有允许我这样做的构造函数.然而,有一个类成员被称为what()可以传递一些信息.
我怎么能(我可以?)将文本传递给a的派生类,std::exception以便通过我的异常类传递信息,所以我可以在代码中的某处说:
throw My_Exception("Something bad happened.");
Run Code Online (Sandbox Code Playgroud)
tun*_*2fs 62
我使用以下类作为我的异常,它工作正常:
class Exception: public std::exception
{
public:
/** Constructor (C strings).
* @param message C-style string error message.
* The string contents are copied upon construction.
* Hence, responsibility for deleting the char* lies
* with the caller.
*/
explicit Exception(const char* message):
msg_(message)
{
}
/** Constructor (C++ STL strings).
* @param message The error message.
*/
explicit Exception(const std::string& message):
msg_(message)
{}
/** Destructor.
* Virtual to allow for subclassing.
*/
virtual ~Exception() throw (){}
/** Returns a pointer to the (constant) error description.
* @return A pointer to a const char*. The underlying memory
* is in posession of the Exception object. Callers must
* not attempt to free the memory.
*/
virtual const char* what() const throw (){
return msg_.c_str();
}
protected:
/** Error message.
*/
std::string msg_;
};
Run Code Online (Sandbox Code Playgroud)
obm*_*arg 55
如果你想使用字符串构造函数,你应该继承std :: runtime_error或std :: logic_error,它实现了一个字符串构造函数并实现了std :: exception :: what方法.
然后,它只是从新的继承类调用runtime_error/logic_error构造函数的情况,或者如果您使用的是c ++ 11,则可以使用构造函数继承.
小智 8
这个怎么样:
class My_Exception : public std::exception
{
public:
virtual char const * what() const { return "Something bad happend."; }
};
Run Code Online (Sandbox Code Playgroud)
或者,如果您愿意,创建一个接受描述的构造函数...
如果您的目标是创建一个异常,以免引发一般性异常(cpp:S112),则可能只想使用using声明公开从(C ++ 11)继承的异常。
这是一个最小的示例:
#include <exception>
#include <iostream>
struct myException : std::exception
{
using std::exception::exception;
};
int main(int, char*[])
{
try
{
throw myException{ "Something Happened" };
}
catch (myException &e)
{
std::cout << e.what() << std::endl;
}
return{ 0 };
}
Run Code Online (Sandbox Code Playgroud)
正如Kilian在评论部分中指出的那样,该示例取决于std :: exception的特定实现,该实现提供的构造函数比此处提到的要多。
为了避免这种情况,您可以使用标头中预定义的任何便利类<stdexcept>。请参阅这些“ 异常类别 ”以获取灵感。
| 归档时间: |
|
| 查看次数: |
61089 次 |
| 最近记录: |