Exception Handling constructor

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
 
E

Erik Wikström

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.

When the end of a function try block handler in a constructor is reached
the exception is rethrown (which is why terminate() is called), so the
effects should be the same as throwing an exception in a normal
constructor, i.e. there is no object obj1.
And in case if the constructor has a parameter initialization list,
how should the try{} catch(){} be.

struct Foo
{
int nr;
Foo(int n)
try
: nr(n)
{
// So some more construction
}
catch(std::exception& e)
{
// Handle exception
}
};

Notice that the initialisation list comes between the 'try' and the '{'.
If an exception is thrown either in the initialisation list of the body
of the try-block control will be transferred to the appropriate catch-
block.
 
S

Sunil Varma

Thanks a lot.
It works fine.
If a constructor of a class is in a try block, then it's better
creating an object dynamically than creating it statically, for that
class.

Sunil
 
D

Daniel T.

Sunil Varma said:
If a constructor of a class is in a try block, then it's better
creating an object dynamically than creating it statically, for that
class.

Not necessarily, the two concepts aren't related. For example, several
of the vector constructors can throw, yet no one recommends creating
them dynamically rather than statically...
 
J

James Kanze

If a constructor of a class is in a try block, then it's
better creating an object dynamically than creating it
statically, for that class.

Just the opposite, I'd say. If you allocate using new, and
something throws later, you have to delete in the throw block.
Except that in many cases, there will be no pointer to the
object in the throw block. In general:

-- don't allocate dynamically unless you have to;

-- if the lifetime of the object is local (e.g. you have to
allocate dynamically because the object is polymorphic,
although it has the same lifetime as a normal local object),
use a smart pointer (boost::scoped_ptr or std::auto_ptr);
and

-- if the lifetime of the object is really dynamic, keep it in
a smart pointer until it has been "exported"---there is a
pointer to the object where ever the pointer to the object
is expected to be. (std::auto_ptr is very good for this.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top