C++异常详解

一.异常概念

当一个函数发现自己处理错误时抛出的异常,让函数调用者间接处理错误。

1.throw:

当问题出现时,程序会抛出一个异常.这是通过使用 throw 关键字来完成的。

2.catch:

在您想要处理问题的地方,通过异常处理程序捕获异常.catch 关键字用于捕获异常,可以有多个catch进行捕获。

3.try:

try 块中的代码标识将被激活的特定异常,它后面通常跟着一个或多个 catch 块。

二.异常的使用

1 异常抛出和匹配原则

异常通过抛出对象引发,激活与处理对象匹配且最近的catch。
抛出异常对象后,生成拷贝对象传给catch(与函数传值类似)。也可以抛出派生类使用基类捕捉。如果到达main函数的栈,依旧没有匹配的,则终止程序。

2.异常的重新抛出

catch也可以再次抛出异常,当异常在当前catch中无法处理的时候,可以再次抛出catch给更外层的catch.

3.异常安全

构造和析构函数中最好不用catch,防止对象初始化不完整以及资源泄漏。
以及在lock和unlock中使用catch导致死锁。c++使用RAII来解决。

4.异常规范

函数后面接throw,列出该函数可能抛出的异常类型;
throw()表示不抛出异常;
若无异常接口声明表示可能抛出的所有异常类型;

// 这里表示这个函数会抛出A/B/C/D中的某种类型的异常
void fun() throw(A,B,C,D);
// 这里表示这个函数只会抛出bad_alloc的异常
void* operator new (std::size_t size) throw (std::bad_alloc);
// 这里表示这个函数不会抛出异常
void* operator delete (std::size_t size, void* ptr) throw();
// C++11 中新增的noexcept,表示不会抛异常
thread() noexcept;
thread (thread&& x) noexcept;

三.自定义异常体系

在项目中会对异常进行规范管理,所以实际中都会定义一套继承的规范体系。这样抛出的就是继承的派生类对象,捕获一个基类就可以了。
在这里插入图片描述

// 服务器开发中通常使用的异常继承体系

class Exception

{

public:
 Exception(const string& errmsg, int id)
 :_errmsg(errmsg)
 ,_id(id)
 {}
 virtual string what() const

 {
 return _errmsg;
 }

protected:
 string _errmsg;
 int _id;
};

class SqlException : public Exception

{

public:
 SqlException(const string& errmsg, int id, const string& sql)
 :Exception(errmsg, id)
 , _sql(sql)
 {}
 virtual string what() const

 {
 string str = "SqlException:";
 str += _errmsg;
 str += "->";
 str += _sql;
 return str;
 }

private:
 const string _sql;
};

class CacheException : public Exception

{

public:
 CacheException(const string& errmsg, int id)
 :Exception(errmsg, id)
 {}
 virtual string what() const

 {
 string str = "CacheException:";
 str += _errmsg;
 return str;
 }
};

class HttpServerException : public Exception

{

public:
 HttpServerException(const string& errmsg, int id, const string& type)
 :Exception(errmsg, id)
 , _type(type)
 {}
 virtual string what() const

 {
 string str = "HttpServerException:";
 str += _type;
 str += ":";
 str += _errmsg;
 return str;
 }

private:
 const string _type;
};

void SQLMgr()
{
 srand(time(0));
 if (rand() % 7 == 0)
 {
 throw SqlException("权限不足", 100, "select * from name = '张三'");
 }
 //throw "xxxxxx";

}

void CacheMgr()
{
 srand(time(0));
 if (rand() % 5 == 0)
 {
 throw CacheException("权限不足", 100);
 }
 else if (rand() % 6 == 0)
 {
 throw CacheException("数据不存在", 101);
 }
 SQLMgr();
}

void HttpServer()
{
 // ...

 srand(time(0));
 if (rand() % 3 == 0)
 {
 throw HttpServerException("请求资源不存在", 100, "get");
 }
 else if (rand() % 4 == 0)
 {
 throw HttpServerException("权限不足", 101, "post");
 }
 CacheMgr();
}

int main()
{
 while (1)
 {
 this_thread::sleep_for(chrono::seconds(1));
 try{
 HttpServer();
 }
 catch (const Exception& e) // 这里捕获父类对象就可以

 {
 // 多态

 cout << e.what() << endl;
 }
 catch (...)
 {
 cout << "Unkown Exception" << endl;
 }
 }
 return 0;
}

四.C++标准库的异常体系

C++ 提供了一系列标准的异常,定义在 中,我们可以在程序中使用这些标准的异常。它们是以父
子类层次结构组织起来的,如下所示:
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值