S
Sunil Varma
Hi,
Here is a piece of code where the constructor throws an exception.
class A
{
int n;
public:
A()
try{
{
throw 10;
}
}
catch(const int &e)
{
cerr<<""<<endl;
}
A(const int &i)
try{
{
if(i == 0)
throw 10;
n = i;
}
}
catch(const int &e)
{
cerr<<"Exception raised in parameterised constructor"<<endl;
}
int get_value()
{
return n;
}
};
void main()
{
A obj(10);
cout<<obj.get_value()<<endl;
A obj1(0);
}
When execute the above code I got the following output on g++
compiler.
10
Exception raised in parameterised constructor
terminate called after throwing an instance of 'i'
What is the logical state of an object if the constructor throws an
exception, here the object obj1.
And in case if the constructor has a parameter initialization list,
how should the try{} catch(){}
be.
Thanks in advance.
Regards
Sunil
Here is a piece of code where the constructor throws an exception.
class A
{
int n;
public:
A()
try{
{
throw 10;
}
}
catch(const int &e)
{
cerr<<""<<endl;
}
A(const int &i)
try{
{
if(i == 0)
throw 10;
n = i;
}
}
catch(const int &e)
{
cerr<<"Exception raised in parameterised constructor"<<endl;
}
int get_value()
{
return n;
}
};
void main()
{
A obj(10);
cout<<obj.get_value()<<endl;
A obj1(0);
}
When execute the above code I got the following output on g++
compiler.
10
Exception raised in parameterised constructor
terminate called after throwing an instance of 'i'
What is the logical state of an object if the constructor throws an
exception, here the object obj1.
And in case if the constructor has a parameter initialization list,
how should the try{} catch(){}
be.
Thanks in advance.
Regards
Sunil