c++ logic_error() 异常的使用方法
*** 简单的用法就是如下所示. 抛出一个logic_error()异常,接住,展示.
$ cat main.cpp
#include
#include
using namespace std;
int main()
{
try
{
// logic_error e("test this");
logic_error e(string("test this ")+"and that"); //显示了字符串类可以直接用+来连接
throw e; //exception 用法, 如此构造和抛出异常
}
catch(exception &ex) // exception 的用法,如此接受异常
{
printf("catch %s\n",ex.what()); //exception 用法,如此访问异常
}
return 0;
}
不爽的地方是ctags认不准这种结构了, 在vim中查看很不方便.
在gdb 中也不让查看. 非常不爽!
(gdb) p e
$1 =
(gdb) ptype e
type = class std::logic_error {
}
*** 那这个logic_error 到底是啥呢?
有时候,c++ 的封装确实不好说是好是坏!
logic_error 是一个exception 的继承类,
在/usr/include/c++/9/stdexcept 文件中定义.
通过qtcreator 找到, 有时IDE 确实在找定义方面更准更强!
// 下面内容有简化
class logic_error : public exception
{
char * _M_msg; //保存信息
public:
logic_error(const string& __arg) _GLIBCXX_TXN_SAFE;
logic_error(const char*) _GLIBCXX_TXN_SAFE;
logic_error(logic_error&&) noexcept;
logic_error& operator=(logic_error&&) noexcept;
logic_error(const logic_error&) = default;
logic_error& operator=(const logic_error&) = default;
virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
/** Returns a C-style character string describing the general cause of
* the current error (the same string passed to the ctor). */
virtual const char*
what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
};
可以看出它主要是一些构造函数 和 what() 函数, 无它了, 再看一下exception 类
class exception
{
public:
exception() _GLIBCXX_NOTHROW { }
virtual ~exception() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
exception(const exception&) = default;
exception& operator=(const exception&) = default;
exception(exception&&) = default;
exception& operator=(exception&&) = default;
/** Returns a C-style character string describing the general cause
* of the current error. */
virtual const char*
what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
};
看来exception 就是一个接口类,logic_error是其一个实现类了.
会像示例那样使用就可以了.