构造函数与throw、catch机制的问题
本帖最后由 fengzhr 于 2013-08-15 16:37:36 编辑
#include <iostream>
using namespace std;
class ageError
{
public:
ageError(int i):_age(i){}
int value(){return _age;}
private:
int _age;
};
class man
{
public:
man(int age)
{
try{
if (age<0) throw ageError(age);
_age=age;
}
catch(ageError &ae)
{
cerr << "i cannot handle this!!!";
cerr << "age is " << ae.value() << endl;
}
}
private:
int _age;
};
void main()
try{
man tman=man(-1);
system("pause");
}
catch(ageError &ae)
{
cerr << "age can not be negative!!!";
cerr << "age is " <<ae.value() <<endl;
system("pause");
}
#include <iostream>
using namespace std;
class ageError
{
public:
ageError(int i):_age(i){}
int value(){return _age;}
private:
int _age;
};
class man
{
public:
man(int age)
try{
if (age<0) throw ageError(age);
_age=age;
}
catch(ageError &ae)
{
cerr << "i cannot handle this!!!";
cerr << "age is " << ae.value() << endl;
}
private:
int _age;
};
void main()
try{
man *tman=new man(-1);
system("pause");
}
catch(ageError &ae)
{
cerr << "age can not be negative!!!";
cerr << "age is " <<ae.value() <<endl;
system("pause");
}