//异常的参数传递
#i nclude <iostream>
#i nclude <string>
using namespace std;
class my_exception
{
public:
my_exception(int i=0)
{
cout<<"######"<<endl;
_i=i;
}
my_exception(const my_exception& obj)
{
_i=obj._i+1;
cout<<'#'<<_i<<endl;
}
my_exception& operator=(const my_exception&obj)
{
if(this!=&obj)
{
_i=obj._i+1;
}
return *this;
}
void print()
{
cout<<'$'<<_i<<endl;
}
~my_exception()
{
cout<<"-----"<<endl;
}
private:
int _i;
};
int main()
{
try
{
throw my_exception(1);
}
catch(my_exception err)
{
err.print();
}
return 0;
}
//C++标准要求throw表达式初始化一个临时对象,这个对象一直存在,直到该异
常被完全处理了为止。
=========
//异常的参数传递
#i nclude <iostream>
#i nclude <string>
using namespace std;
class my_exception
{
public:
my_exception(int i=0)
{
cout<<"######"<<endl;
_i=i;
}
my_exception(const my_exception& obj)
{
_i=obj._i+1;
cout<<'#'<<_i<<endl;
}
my_exception& operator=(const my_exception&obj)
{
if(this!=&obj)
{
_i=obj._i+1;
}
return *this;
}
void print()
{
cout<<'$'<<_i<<endl;
}
~my_exception()
{
cout<<"-----"<<endl;
}
private:
int _i;
};
int main()
{
my_exception ex(1);
try
{
throw ex;
}
catch(my_exception err) //catch(my_exception& err) 可以避免后面
的一次创建对象的工作。
{
err.print();
}
return 0;
}
//C++标准要求throw表达式初始化一个临时对象,这个对象一直存在,直到该异
常被完全处理了为止。
========