C++异常
C++异常基本概念
异常是一种处理错误的方式,当一个函数发现自己无法处理的错误的时候就可以抛出异常。下面是C++有关异常的几个关键字:
下面通过一段代码来进行说明:
// 异常
double Division(int a, int b)
{
if (b == 0)(
throw "Division by zero condition!";
else
return ((double)a / (double)b);
}
void Func()
{
int len, time;
cin >> len >> time;
cout << Division(len, time);
}
int main()
{
try
{
Func();
}
catch (const char* errmsg)
{
cout << errmsg << endl;
}
return 0;
}
上面的代码是除法程序中当被除数为0的时候就出现异常,throw后面是异常抛出对象,这个对象可以是任意类型的对象;当函数运行起来的时候,如果函数没有发生任何异常,就会跳过catch部分;那么异常的抛出和匹配规则是怎么样的?
-
异常是由抛出对象而引发的,该对象的类型决定了应该激活哪个catch的处理代码,而其他的catch处理代码会被忽略,如果抛出的异常没有任何一个catch可以匹配,那么会直接终止程序
-
被选中的处理代码是调用链中与该对象类型匹配且离抛出异常位置最近的一个 ,比如上面的代码中,Division函数抛出异常后,如果在Func函数中有捕获,在main函数中也有捕获,那么会优先选择距离抛出异常位置最近的一个,也就是Func函数,当然这个位置近不是代码书写位置,而是在调用关系上
// 异常
double Division(int a, int b)
{
if (b == 0)
throw "Division by zero condition!";
else
return ((double)a / (double)b);
}
void Func()
{
int len, time;
cin >> len >> time;
try
{
cout << Division(len, time);
}
catch(...)
{
throw;
}
}
int main()
{
try
{
Func();
}
catch (const char* errmsg)
{
cout << errmsg << endl;
}
return 0;
}
-
代码也可以类似上面写,Func函数捕获异常,如果异常返回对象类型过多,并且多个类型的异常处理方式相同,那么在catch捕获部分可以用…来代替抛出对象的类型,然后捕获后再次throw。throw的时候如果上面也不写就是将捕获到的异常再次重新抛出。
-
抛出和捕获并不是完全匹配,可以抛出的派生类对象,使用基类捕获。
自定义异常体系
如果每次抛异常只是一个简单的字符串或者数字,那么在一些很多的大型项目中显得很鸡肋,很多公司都会自己定义异常规范。
在异常的抛出中,构造对象进行抛出比较实用:
// 服务器开发中通常使用的异常继承体系
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)
{
try {
Sleep(1000);
HttpServer();
}
catch (const Exception& e) // 这里捕获父类对象就可以
{
// 多态
cout << e.what() << endl;
}
catch (...)
{
cout << "Unkown Exception" << endl;
}
}
return 0;
}
前面提到了可以抛出派生类的对象,然后用基类捕获,这里就可以利用C++特性之一多态进行程序设计,在捕获对象时使用父类对象进行捕获,而抛出对象中可以抛出派生类,使用多态实现不同异常的捕获。
C++异常的优缺点
C++异常的优点:
- 异常的设计极大方便了调试工作,首先异常发生不会导致程序直接退出,比如构造函数如果没有异常,其本身也没有返回值,就没办法通过返回值表示错误
- 异常相比返回值,构造异常对象可以表示更多的错误信息,更好帮助找到程序的问题。
- 异常相比返回值,如果函数调用链比较长,那么异常可以直接跳转到捕捉位置进行异常捕捉和处理,如果是返回值就需要经过每一层调用达到效果
- 有很多第三方库也包含异常,如果想要使用这些库,就需要使用异常
C++异常的缺点:
- 如果返回值可以让函数的返回层层递进,那么异常就会导致程序的执行流变得混乱,跟踪调试以及分析程序会变的困难
- C++没有垃圾回收机制,资源需要自己管理,异常容易导致内存泄漏,死锁等异常安全问题,这个之后的智能指针部分详细讨论
- C++的异常规范并不是在语法层面强制的,有很多的自定义异常机制,比较混乱
针对C++异常的规范,这里说一下:
上面的代码中在函数声明中出现,实际上在声明部分和定义部分出现都是没问题的,但是一定要匹配。
论
- C++的异常规范并不是在语法层面强制的,有很多的自定义异常机制,比较混乱
针对C++异常的规范,这里说一下:
[外链图片转存中…(img-iYgOZmid-1682907829541)]
上面的代码中在函数声明中出现,实际上在声明部分和定义部分出现都是没问题的,但是一定要匹配。