1、C+ 异常1 使用函数的返回值来标识错误状态2 异常处理方式,对象代表一种错误状态抛出一个异常对象throw 对象;如果一个函数没有指明抛出什么异常,则代表可能抛出任何异常如果一个程序抛出异常,系统不去处理,则最终会调用 terminate01exception.cpp#include#include#includeusing namespace std;void foo()srand(time(NULL);int num=rand()%4;if(0=num)cout#include#includeusing namespace std;void foo()srand(time(NULL);i
2、nt num=rand()%4;if(0=num)cout04systemexception.cpp#include#include#includeusing namespace std;int main()int* pi=reinterpret_cast(malloc(0xffffffff);coutmsg=msg;virtual const char* what()const throw()return msg.c_str();05userexception.cpp#includeusing namespace std;class DmsClientExceptionstring msg;
3、public:DmsClientException(string msg=“dms client exception“)this-msg=msg;virtual const char* what()const throw()return msg.c_str();class DmsReaderException:public DmsClientExceptionpublic:DmsReaderException(string msg=“dms reader exception“):DmsClientException(msg);void readLogs()throw(DmsReaderException)if(1=1)throw DmsReaderException(“back up file failed“);int main()trytryreadLogs();catch(DmsReaderException/ 不能处理就继续上抛cout“这层无能为力,请求上层处理“endl; throw ;catch(DmsReaderExceptioncout“app continue“endl;