异常
关于异常/错误的处理,C语言时期的方法是通过错误码的形式来处理错误。错误码类似于一个map,C语言内核会为每一个错误码附带一个错误信息,发生异常后便会给出错误码,接着我们需要拿着错误码去找错误信息,比较麻烦。在C++中异常处理机制得到了改善,使用的是异常的抛出与捕获方法。
当程序出现问题的时候,我们通过抛出(throw)一个对象来触发一个异常,接着由catch判断类型是否符合。每一个try都至少会匹配一个catch,否则就会报错。异常会优先找离自己最近的那个类型相同的catch,当发生了throw,程序就不会执行throw以后的程序,则是直接去找catch了,类似于return,只不过return是返回到栈的上一层,catch会先在本函数里面找catch,如果没找到符合的,会沿着调用链往上寻找,当他开始往上寻找,沿着调用链创建的对象都将被销毁。如果一直到main函数都没找到匹配的catch子句,程序会调用标准库的terminate函数。
#include <iostream>
using namespace std;
float Divide(int a, int b)
{
try
{
if (b == 0)
{
string s("除0了!!!!");
throw s;
}
else
{
return (double)a / (double)b;
}
}
catch (int id)
{
cout << "id" << endl;
}
}
void Func()
{
int a, b;
cin >> a >> b;
try {
cout << Divide(a, b) << endl;
}
catch (int errid)
{
cout << errid<< endl;
}
}
int main()
{
while (1)
{
try
{
Func();
}
catch (string& error)
{
cout << error << endl;
}
}
return 0;
}
一般来说,抛出对象和catch类型是完全一致的,但是这个类型也允许一些例外,例如允许从非常量转为常量,数组转换为指向数组元素类型的指针,函数被转换为指向函数的函数指针,允许派生类向基类的转换,最后一点非常实用,在实际的工作流中都是用这个方法进行设计了。
class Exception
{
public :
Exception(const string& errmsg, int id)
: _errmsg(errmsg)
, _id(id)
{
}
virtual string what() const
{
return _errmsg;
}
int getid() const
{
return _id;
}
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 HttpException : public Exception
{
public :
HttpException(const string& errmsg, int id, const string& type)
: Exception(errmsg, id)
, _type(type)
{
}
virtual string what() const
{
string str = "HttpException:";
str += _type;
str += ":";
str += _errmsg;
return str;
}
private:
const string _type;
};
void SQLMgr()
{
if (rand() % 7 == 0)
{
throw SqlException("权限不⾜", 100, "select * from name = '张三'");
}
else
{
cout << "SQLMgr 调⽤成功" << endl;
}
}
void CacheMgr()
{
if (rand() % 5 == 0)
{
throw CacheException("权限不⾜", 100);
}
else if (rand() % 6 == 0)
{
throw CacheException("数据不存在", 101);
}
else
{
cout << "CacheMgr 调⽤成功" << endl;
}
SQLMgr();
}
void HttpServer()
{
if (rand() % 3 == 0)
{
throw HttpException("请求资源不存在", 100, "get");
}
else if (rand() % 4 == 0)
{
throw HttpException("权限不⾜", 101, "post");
}
else
{
cout << "HttpServer调⽤成功" << endl;
}
CacheMgr();
}
int main()
{
srand(time(0));
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;
}
异常抛出后,后面的代码就不会执行了,会逐步的把在局部域内的对象给销毁,如果前面申请了资源,但是释放在throw后面,就会造成了内存泄漏的问题。如果要用逻辑程序来解决这个问题会很麻烦,因此就要用到我们下一章会提到的RAII方式。catch(...)表示的是啥都捕获。throw表示的是catch里面捕获到啥抛出啥,去寻找下一个符合类型的catch.
double Divide(int a, int b)
{
// 当b == 0时抛出异常
if (b == 0)
{
throw "Division by zero condition!";
}
return(double)a / (double)b;
}
void Func()
{
// 这⾥可以看到如果发⽣除0错误抛出异常,另外下⾯的array没有得到释放。
// 所以这⾥捕获异常后并不处理异常,异常还是交给外层处理,这⾥捕获了再
// 重新抛出去。
int* array = new int[10];
try
{
int len, time;
cin >> len >> time;
cout << Divide(len, time) << endl;
}
catch(...)
{
// 捕获异常释放内存
cout << "delete []" << array << endl;
delete[] array;
throw; // 异常重新抛出,捕获到什么抛出什么
}
cout << "delete []" << array << endl;
delete[] array;
}
int main()
{
try
{
Func();
}
catch(const char* errmsg)
{
cout << errmsg << endl;
}
catch(const exception & e)
{
cout << e.what() << endl;
}
catch(...)
{
cout << "Unkown Exception" << endl;
}
return 0;
}
对于用户和编译器来说,如果能提前知道某个程序能不能抛出异常,我们就能对应的设计程序来简化代码,提高效率,在C++11中加入noexcept,函数参数列表后面加上noexcept表示不会抛出异常,啥都不加的话表示可能抛异常。一个函数如果用noexcept修饰了,但是里面还是有throw相关内容,编译还是会通过,但是如果这个函数触发了异常,程序会调用terminate终止程序,而非常见的catch捕捉机制。noexcept(expression)还可以用来检测一个表达式是否会抛异常,可能返回false(0),不会返回true(1)。noexcept不会检查内部的具体函数的
/ C++98
// 这⾥表⽰这个函数只会抛出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
size_type size() const noexcept;
iterator begin() noexcept;
const_iterator begin() const noexcept;
double Divide(int a, int b) noexcept
{
// 当b == 0时抛出异常
if (b == 0)
{
throw "Division by zero condition!";
} r
eturn (double)a / (double)b;
} i
nt main()
{
try
{
int len, time;
cin >> len >> time;
cout << Divide(len, time) << endl;
} c
atch (const char* errmsg)
{
cout << errmsg << endl;
} c
atch (...)
{
cout << "Unkown Exception" << endl;
} i
nt i = 0;
cout << noexcept(Divide(1,2)) << endl;
cout << noexcept(Divide(1,0)) << endl;
cout << noexcept(++i) << endl;
return 0;
}
智能指针
以前,当我们在new的时候,我们得匹配delete,new[]的时候,我们需要delete[]来进行匹配。但是此时如果抛异常了,或者提前return了,那么我们可能没有相应的可以释放内存的程序,就会导致内存泄漏。此时,我们使用智能指针,就能解决这个问题。
官方的智能指针包括auto_ptr,unique_ptr,shared_ptr,weak_ptr。其中auto_ptr完全不建议用,有指针悬空的问题。unique_ptr不允许拷贝,只允许移动。shared_ptr泛用性比较强,可以多个对象管理同一块空间。weak_ptr是为了解决shared_ptr可能的循环引用问题。
RAII与智能指针的设计思路
RAII是Resource Accqusition Initiallization的缩写,意为是资源获取初始化,本质上是将获取的动态资源利用对象生命周期来进行管理,这里的资源可以是内存,文件指针,网络连接等等,RAII获取资源后将其委托给一个对象,接着控制对资源的访问,资源在对象的声明周期时有效,对象析构的时候会释放资源,这样我们就能避免资源泄露。一般来说,智能指针还会重载 operator*/operator->/operator[] 等运算符。下面是关于shared_ptr的部分重新实现。
#include <functional>
namespace yu
{
template <class T>
class shared_ptr
{
public:
shared_ptr(T* ptr)
: _ptr(ptr),
_ptr_count(new int(1))
{
}
template <class D>
shared_ptr(T* ptr, D del)
: _ptr(ptr),
_ptr_count(new int(1)),
_del(del)
{
}
shared_ptr(const shared_ptr<T>& sp)
:_ptr(sp._ptr),
_ptr_count(sp._ptr_count),
_del(sp._del)
{
(*_ptr_count)++;
}
void release()
{
if (--(*_ptr_count) == 0)
{
delete _ptr;
delete _ptr_count;
}
}
~shared_ptr()
{
release();
}
shared_ptr& operator=(const shared_ptr& sp)
{
if (_ptr != sp._ptr)
{
release();
_ptr == sp._ptr;
_del = sp._del;
_ptr_count = sp._ptr_count;
(*_ptr_count)++;
}
return *this;
}
T& operator*()
{
return *_ptr;//返回解引用的内容
}
T* operator->()
{
return _ptr;//返回地址
}
int use_count() const
{
return *_ptr_count;
}
private:
T* _ptr;
int* _ptr_count;
function<void(T*)> _del = [](T* ptr) {delete ptr;};//用包装器进行包装,void是返回类型,T*是参数
};
}
int main()
{
int* a = new(int);
string* s = new(string);
*a = 1;
cout << *a << endl;
yu::shared_ptr<int> sp1 = a;
cout << *a << endl;
//yu::shared_ptr<int> sp2 = a;报错!因为这样构造出来的对象未拥有同一个_ptr_count,虽然指向同一块空间,但是计数紊乱。后面析构的时候也会多次析构!智能通过sp1来构造
yu::shared_ptr<int> sp2 = sp1;
*sp2 = 4;
cout << *sp2 << endl;
return 0;
}
shared_ptr⼤多数情况下管理资源⾮常合适,⽀持RAII,也⽀持拷⻉。但是在循环引⽤的场景下会
导致资源没得到释放内存泄漏,所以我们要认识循环引⽤的场景和资源没释放的原因,并且学会使
⽤weak_ptr解决这种问题。
• 如下图所述场景,n1和n2析构后,管理两个节点的引⽤计数减到1
-
右边的节点什么时候释放呢,左边节点中的_next管着呢,_next析构后,右边的节点就释放了。
-
_next什么时候析构呢,_next是左边节点的的成员,左边节点释放,_next就析构了。
比特就业课3. 左边节点什么时候释放呢,左边节点由右边节点中的_prev管着呢,_prev析构后,左边的节点就释
放了。
- _prev什么时候析构呢,_prev是右边节点的成员,右边节点释放,_prev就析构了。
• ⾄此逻辑上成功形成回旋镖似的循环引⽤,谁都不会释放就形成了循环引⽤,导致内存泄漏
• 把ListNode结构体中的_next和_prev改成weak_ptr,weak_ptr绑定到shared_ptr时不会增加它的
引⽤计数,_next和_prev不参与资源释放管理逻辑,就成功打破了循环引⽤,解决了这⾥的问题

weak_ptr不支持RAII,weak_ptr也没有重载operator*和operator->等,因为他不参与资源管理,那么如果他绑定的shared_ptr已经释放了资源,那么他去访问资源就是很危险的。weak_ptr⽀持expired检查指向的资源是否过期,use_count也可获取shared_ptr的引⽤计数,weak_ptr想访问资源时,可以调⽤lock返回⼀个管理资源的shared_ptr,如果资源已经被释放,返回的shared_ptr是⼀个空对象,如果资源没有释放,则通过返回的shared_ptr访问资源是安全的。