首页 > 编程 > C++ > 正文

c++异常处理--创建自己的异常处理类

2019-11-06 06:57:02
字体:
来源:转载
供稿:网友
复习了一下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;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选