传统C语言错误异常的方式
C++异常处理方式
try
{
}catch( ExceptionName e1 )
{
}catch( ExceptionName e2 )
{
}catch( ExceptionName eN )
{
}
异常的使用

#include<iostream>
using namespace std;
double Division(int a, int b)
{
if (b == 0)
{
throw "除0错误!";
}
else
{
return double(a / b);
}
}
void Func()
{
int a,b;
cin >> a >> b;
cout << Division(a, b) << endl;
}
int main(void)
{
try
{
Func();
}
catch (const char* errmsg)
{
cout << errmsg << endl;
}
catch (...)
{
cout << "未知错误" << endl;
}
return 0;
}
异常的重新抛出
#include<iostream>
using namespace std;
double Division(int a, int b)
{
if (b == 0)
{
throw "除0错误!";
}
else
{
return double(a / b);
}
}
void Func()
{
int* arr = new int[10];
int a, b;
cin >> a >> b;
cout << Division(a, b) << endl;
cout << "delete[]" << arr << endl;
delete[]arr;
}
int main(void)
{
try
{
Func();
}
catch (const char* errmsg)
{
cout << errmsg << endl;
}
catch (...)
{
cout << "未知错误" << endl;
}
return 0;
}
#include<iostream>
using namespace std;
double Division(int a, int b)
{
if (b == 0)
{
throw "除0错误!";
}
else
{
return double(a / b);
}
}
void Func()
{
int* arr = new int[10];
try
{
int a, b;
cin >> a >> b;
cout << Division(a, b) << endl;
}
catch(...){
cout << "delete[]" << arr << endl;
delete[]arr;
throw;
}
cout << "delete[]" << arr << endl;
delete[]arr;
}
int main(void)
{
try
{
Func();
}
catch (const char* errmsg)
{
cout << errmsg << endl;
}
catch (...)
{
cout << "未知错误" << endl;
}
return 0;
}
异常安全问题
自定义异常体系
#include<iostream>
#include<Windows.h>
#include<string>
using namespace std;
double Division(int a, int b)
{
if (b == 0)
{
throw "除0错误!";
}
else
{
return double(a / b);
}
}
void Func()
{
int* arr = new int[10];
try
{
int a, b;
cin >> a >> b;
cout << Division(a, b) << endl;
}
catch(...){
cout << "delete[]" << arr << endl;
delete[]arr;
throw;
}
cout << "delete[]" << arr << endl;
delete[]arr;
}
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 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;
};
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;
}
};
void SQLMgr()
{
srand(time(0));
if (rand() % 7 == 0)
{
throw sqlException("权限不足", 100, "select * from name = '张三'");
}
}
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(500);
HttpServer();
}
catch (const Exception& e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "Unkown Exception" << endl;
}
}
return 0;
}
void SeedMsg(const string& s)
{
srand(time(0));
if (rand() % 3 == 0)
{
throw HttpServerException("网络错误", 500, "get");
}
else if (rand() % 4 == 0)
{
throw HttpServerException("权限不足", 101, "post");
}
cout << "发送成功:" << s << endl;
}
void HttpServer()
{
string str = "今晚一起看电影怎么样?";
int n = 3;
while (n--)
{
try
{
SeedMsg(str);
break;
}
catch (const Exception& e)
{
if (e.getid() == 500 && n > 0)
{
continue;
}
else
{
throw e;
}
}
}
}
C++标准库的异常体系

