Exception Handling constructor



 DEVELOP > c-Plus-Plus > Exception Handling constructor

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Sunil Varma"
Date: 13 Jan 2008 03:32:10 AM
Object: Exception Handling constructor
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
.

User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: Exception Handling constructor 13 Jan 2008 05:37:55 AM
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
.
User: "Sunil Varma"

Title: Re: Exception Handling constructor 13 Jan 2008 06:22:07 AM
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=F6m <Erik-wikst...@telia.com> wrote:

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 =3D=3D 0)
throw 10;
n =3D 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=F6m

.
User: "James Kanze"

Title: Re: Exception Handling constructor 14 Jan 2008 04:19:56 AM
On Jan 13, 1:22 pm, Sunil Varma <sunil....@gmail.com> wrote:

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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.

User: "Daniel T."

Title: Re: Exception Handling constructor 13 Jan 2008 09:27:45 AM
Sunil Varma <sunil.s51@gmail.com> wrote:

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...
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER