#include <iostream>
#include <cstdlib>
using namespace std;
//总异常
class BankException{};
//业务异常
class BusinessException : public BankException
{
};
class TransferException : public BusinessException
{
};
class LoginException : public BusinessException
{
};
class DepositException : public BusinessException
{
};
//...
//系统异常
class SystemException : public BankException
{
};
class IOException : public SystemException
{
};
class SQLException : public IOException
{
};
class FileNotFoundException : public IOException
{
};
void dao() throw(SQLException)
{
cout << "访问数据库" << endl;
srand(time(0));
if(rand()%2) throw SQLException();
}
void transfer() throw (TransferException)
{
cout << "转账" << endl;
try
{
dao();
}
catch(SQLException& e)
{
cout << "处理数据库异常。" << endl;
throw TransferException();
}
}
int main()
{
try
{
transfer();
}
catch(TransferException& e)
{
cout << "处理转账异常。" << endl;
}
}
/* 程序运行结果
转账
访问数据库
处理数据库异常。
处理转账异常。
*/
模拟异常类的编写
最新推荐文章于 2022-07-17 16:18:18 发布