异常处理1

#include<iostream>
#include<string>
using namespace std;
class MyException{
public:
MyException(const string &message):message(message){}
~MyException(){}
const string &getMessage() const {return message;}
private:
string message;
};
class Demo{
public:
Demo(){cout<<"Constructor of Demo"<<endl;}
~Demo(){cout<<"Destructor of Demo"<<endl;}
};
void func() throw(MyException){
Demo d;
cout<<"Throw MyException in func()"<<endl;
throw MyException("exception thrown by func()");
}
int main()
{
cout<<"In main function"<<endl;
try{
func();
}catch(MyException& e){
cout<<"Caught an exception of main()"<<endl;
return 0;
}
}

posted @ 2023-05-19 21:04  不会JAVA的小袁  阅读(13)  评论(0编辑  收藏  举报