复习了一下c++中的异常处理!
继承exception类
class myException : public std::exception
{
public:
explicit myException(std::string meg) : message(meg){}
//exception类中有一个what()虚函数,重新实现它
virtual const char* what(){return message.c_str();}
//不要少了这一行代码
virtual ~myException() throw(){}
private:
std::string message;
}
--------------------------------------------------------------------
1.function() throw(){}
/**函数后面的throw()表示此函数不会发出异常,c++11已将其摒弃,而使用noexcept关键字代替 function() noexcept{}; */
2.function() throw(bad_thing){}
/**函数后面的throw(bad_thing)表示此函数会发出bad_thing类型异常*/
3.function() throw(...){}
/**函数后面的throw(...)表示此函数会发出任意类型的异常*/
4.据<<C++ primer plus>>书中表示,noexcept关键字也可以用作运算符,判断操作数是否可能引发异常,如果操作数可能引发异常,则noexcept返回false,否则返回true。
int function_1(int, int);
int function_2(int, int) noexcept;
noexcept(function_1) --> return false;
noexcept(function_2) --> return true;
c++异常处理--创建自己的异常处理类
最新推荐文章于 2024-05-17 00:12:01 发布