- assert()函数直接终止程序。弊端:用户难以接受
- 返回错误码。弊端:需要程序员自己去查找对应的错误
异常是一种处理错误的方式,当一个函数发现自己无法处理的错误时就可以抛出异常,让函数的直接或间接的调用者处理这个错误。
- throw: 当问题出现时,程序会抛出一个异常。这是通过使用 throw 关键字来完成的。
- catch: 在您想要处理问题的地方,通过异常处理程序捕获异常.catch 关键字用于捕获异常,可以有多个catch进行捕获。
- try: try 块中的代码标识将被激活的特定异常,它后面通常跟着一个或多个 catch 块。
try
{
// 保护的标识代码
}catch( ExceptionName e1 )
{
// catch 块
}catch( ExceptionName e2 )
{
// catch 块
}catch( ExceptionName eN )
{
// catch 块
}
#include
using namespace std;
double division(int x, int y)
{
if (y == 0)
throw "division by zero condition!";
return (double)x / (double)y;
}
void test()
{
int x, y;
cin >> x >> y;
division(x, y);
}
int main()
{
test();
return 0;
}
//输入:1 0
//程序直接终止
#include
using namespace std;
double division(int x, int y)
{
if (y == 0)
throw "division by zero condition!";
return (double)x / (double)y;
}
void test()
{
int x, y;
cin >> x >> y;
try
{
division(x, y);
}
catch (const char* message)
{
cout << message << endl;
}
}
int main()
{
test();
return 0;
}
#include
using namespace std;
double division(int x, int y)
{
if (y == 0)
throw "division by zero condition!";
return (double)x / (double)y;
}
void test()
{
int x, y;
cin >> x >> y;
try
{
division(x, y);
}
catch (const char* message)
{
cout << "test()" << endl;
cout << message << endl;
}
}
int main()
{
try
{
test();
}
catch (const char* message)
{
cout << "main()" << endl;
cout << message << endl;
}
return 0;
}
//输入:1 0
//输出:
//test()
//division by zero condition!
#include
using namespace std;
double division(int x, int y)
{
if (y == 0)
throw "division by zero condition!";
return (double)x / (double)y;
}
void test()
{
int x, y;
cin >> x >> y;
try
{
division(x, y);
}
catch (char message)
{
cout << "test()" << endl;
cout << message << endl;
}
}
int main()
{
try
{
test();
}
catch (const char* message)
{
cout << "main()" << endl;
cout << message << endl;
}
return 0;
}
//输入:1 0
//输出:
//main()
//division by zero condition!
#include
using namespace std;
double division(int x, int y)
{
if (y == 0)
throw "division by zero condition!";
return (double)x / (double)y;
}
void test()
{
int x, y;
cin >> x >> y;
try
{
division(x, y);
}
catch (...)
{
cout << "test()" << endl;
}
}
int main()
{
try
{
test();
}
catch (const char* message)
{
cout << "main()" << endl;
cout << message << endl;
}
return 0;
}
//输入:1 0
//输出:
//test()
#include
using namespace std;
class Exception
{
protected:
int _code; //错误码
std::string _message; //错误信息
public:
Exception(int errcode, const std::string& errmessage)
:_code(errcode)
,_message(errmessage)
{}
~Exception() {}
int getErrcode() const
{
return _code;
}
const string& getMessage() const
{
return _message;
}
};
class DivisionException : public Exception
{
private:
typedef Exception Base;
public:
DivisionException(int errcode, const std::string& errmessage)
:Base(errcode, errmessage)
{}
~DivisionException() {}
};
double division(int x, int y)
{
if (y == 0)
throw DivisionException(1, string("division by zero condition!"));
return (double)x / (double)y;
}
void test()
{
int x, y;
cin >> x >> y;
try
{
division(x, y);
}
catch (const Exception& exception)
{
cout << "errcode:" << exception.getErrcode() << endl << "errmessage:" << exception.getMessage() << endl;
}
}
int main()
{
test();
return 0;
}
#include
using namespace std;
double division(int x, int y)
{
if (y == 0)
throw "division by zero condition!";
return (double)x / (double)y;
}
void test()
{
int x, y;
cin >> x >> y;
try
{
division(x, y);
}
catch (const char* message)
{
cout << "test()" << endl;
cout << message << endl;
throw;
}
catch (...)
{
cout << "unknown exception!" << endl;
}
}
int main()
{
try
{
test();
}
catch (...)
{
cout << "main()" << endl;
}
return 0;
}
//输入:1 0
//输出
//test()
//division by zero condition!
//main()
// 这里表示这个函数会抛出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;
#include
#include
#include
using namespace std;
class Exception
{
protected:
int _code; //错误码
std::string _message; //错误信息
public:
Exception(int errcode, const std::string& errmessage)
:_code(errcode)
,_message(errmessage)
{}
~Exception() {}
public:
int getErrcode() const
{
return _code;
}
const string& getMessage() const
{
return _message;
}
virtual std::string what() const
{
return std::to_string(_code) + _message;
}
};
class SQLException : public Exception
{
private:
typedef Exception Base;
private:
std::string _SQLstatement; //SQL语句错误
public:
SQLException(int errcode, const std::string& errmessage, const string& SQLstatement)
:Base(errcode, errmessage)
,_SQLstatement(SQLstatement)
{}
~SQLException() {}
public:
virtual string what() const
{
string message = "SQLException:" + std::to_string(_code) + " " + _message + "->" + _SQLstatement;
return message;
}
};
class CacheException : public Exception
{
private:
typedef Exception Base;
public:
CacheException(int errcode, const std::string& errmessage)
:Base(errcode, errmessage)
{}
~CacheException() {}
public:
virtual string what() const
{
string message = "CacheException:" + std::to_string(_code) + " " + _message;
return message;
}
};
class HttpServerException : public Exception
{
private:
typedef Exception Base;
private:
std::string _type; //错误类型
public:
HttpServerException(int errcode, const std::string& errmessage, const std::string& errtype)
:Base(errcode, errmessage)
,_type(errtype)
{}
~HttpServerException() {}
public:
virtual string what() const
{
string message = "HttpServerException:" + std::to_string(_code) + " " + _message + "->" + _type;
return message;
}
};
void SQL()
{
srand(time(0));
if (rand() % 7 == 0)
{
throw SQLException(7, "permission denied", "select * from name = '张三'");
}
cout << "success" << endl;
}
void cache()
{
srand(time(0));
if (rand() % 5 == 0)
{
throw CacheException(5, "permissiong denied");
}
else if (rand() % 6 == 0)
{
throw CacheException(6, "data do not exsit");
}
SQL();
}
void httpServer()
{
srand(time(nullptr));
if (rand() % 3 == 0)
throw HttpServerException(3, "Request insufficient resources", "get");
if (rand() % 4 == 0)
throw HttpServerException(4, "permission denied", "post");
cache();
}
void test()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
try
{
httpServer();
}
catch (const Exception& exception)
{
cout << exception.what() << endl;
}
catch (...)
{
cout << "unknown exception!" << endl;
}
}
}
int main()
{
test();
return 0;
}