473,466 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jan 13 '08 #1
4 2893
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kevin Goodsell | last post by:
If I'm writing a class that will be used as an exception, what kinds of things do I need to watch out for? For example, is it necessary to make sure that the members of the class don't throw? ...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
3
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and there is something that I want to ask you about. It says "since the constructor does not return any value, the only way to handle errors that occur...
9
by: David B | last post by:
Why is it so difficult to report bugs to Microsoft? I have a documented bug and an small test example. I don't really see why I should have to pay to tell them about it... Anyway, the...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
1
by: thejackofall | last post by:
Howdy? Good. I am getting this error when the Exception Handling Application Block when it tries to create my exception handler. Here is the error. Constructor on type...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
6
by: asm23 | last post by:
Hi, everyone, I'm learning <<thinking c++>volume Two, and testing the code below. ///////////////////////////////////////////////////////////////////////////// //: C01:Wrapped.cpp #include...
4
by: DHS1 | last post by:
Hey guys. I have a lab that is due in two weeks, but I wanted to start on it now. Problem is, I'm at home during christmas break so I can't ask my professors. Here's my problem: I am given a very...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.