By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

Exception Handling constructor

P: n/a
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
Jan 13 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2008-01-13 10:32, Sunil Varma wrote:
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.

--
Erik Wikström
Jan 13 '08 #2

P: n/a
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

On Jan 13, 4:37 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-01-13 10:32, Sunil Varma wrote:
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.

--
Erik Wikström
Jan 13 '08 #3

P: n/a
Sunil Varma <su*******@gmail.comwrote:
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...
Jan 13 '08 #4

P: n/a
On Jan 13, 1:22 pm, Sunil Varma <sunil....@gmail.comwrote:
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 14 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.