#include <iostream>
#include <cstdlib>
using namespace std;
/********************************************************
* function :visit database *
* param :no *
* return :0 successfule -1 failed *
* author : Daniel *
* ******************************************************/
int visit_db()
{
cout << "visit_db" << endl;
srand(time(0));
if(rand()%2)
{
return 0;
}
return -1;
}
void access_db() throw(const char*)
{
cout << "access db" << endl;
srand(time(0));
if(0 == rand()%2) throw "ERROR";
}
int main()
{
int res = visit_db();
//感觉这里的流程要调整一下,运行的结果看着别扭
if(-1 == res)
{
cout << "access database failed" << endl;
}
try
{
access_db();
}
catch(const char* str)
{
cout << "处理异常" << str << endl;
}
cout << "程序继续运行" << endl;
}
throw、try、catch
最新推荐文章于 2025-06-07 19:24:53 发布
