I following program, While creating an obj, default constructor gets called but two times copy constructor and two destructor gets called. I am not able to understand why this is happening ?
#include <iostream>
#include <exception>
using namespace std;
class sam
{
public :
sam()
{
cout<<"\n Default Constuctor";
}
sam(int a)
{
cout<<"\n Parameterised Constuctor";
}
sam(const sam &obj)
{
cout<<"\n Copy Constuctor";
}
sam & operator = (const sam &obj)
{
cout<<"\n Overloaded assignment operator";
}
~sam()
{
cout<<"\n destructor";
}
};
void fun()
{
try
{
sam obj;
throw obj;
}
catch(char *ptr)
{
cout<<"\n Catch block";
}
catch(sam ex)
{
cout<<"\n fun ";
}
}
int main()
{
fun();
cout<<endl;
return 0;
}
Output is :