473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Exceptions creating Exceptional Problems!

Hi all

I'm facing a strange problem in C++ exceptions.

If an error occurs in a class, throw() is used. But whenever that
happens, the object is destroyed.

In a nutshell, accessing the object in which exception occured is not
being possible in catch block.

Either i'm interpreting the things wrongly, or is it a limitation...

Please suggest some workaround AND|OR clear my mis-interpretation.

A more detailed version of my question can be found at my blog...
http://makuchaku.blogspot.com/2005/0...ceptional.html
Do reply asap...

Thanks,
makuchaku

Jul 22 '05 #1
5 1130
"makuchaku" <ma********@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I'm facing a strange problem in C++ exceptions.

If an error occurs in a class, throw() is used. But whenever that
happens, the object is destroyed.

In a nutshell, accessing the object in which exception occured is not
being possible in catch block.

Either i'm interpreting the things wrongly, or is it a limitation...

Please suggest some workaround AND|OR clear my mis-interpretation.

A more detailed version of my question can be found at my blog...
http://makuchaku.blogspot.com/2005/0...ceptional.html
Do reply asap... The code was short enough that you could have posted it here:
#include<...>
void main()
{
try
{
Socket object(); The above is a function declaration. I guess you meant:
Socket object; object.do_something_else();
}catch(SockException err)
{
err.getCode(); //works
err.getMessage(); //works
//object.cannot_access_this(); <----- this is troublesome! For that to work, 'object' need to be declared outside of/prior to
the try block:
Socket object;
try {
..... }
}


Of course this implies that if you cannot access the object in a
catch clasue if its constructor has thrown an exception. If the
constructor did not execute successfully, there is no object to
be accessed.

Eventually, you may want to use two-step initialization: a
non-throwing constructor that creates an instance in a fail-safe
state, and a separate function to attempt the creation of a
resource (or connection, in this instance).

This very much makes sense to me.
Do you consider this to be a practical problem ?

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2
your idea can be a possible solution.
I also found another way!
int main()
{
Socket *s;
try
{
s = new Socket(ERRORNEOUS_VALUE);
}catch(...)
{
s->memberFunction();
}
}

atleast this works. Now its upto me, how do i design my class.
anyways, thanks for your help.

makuchaku

Jul 22 '05 #3
"makuchaku" <ma********@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
your idea can be a possible solution.
I also found another way!

int main()
{
Socket *s;
try
{
s = new Socket(ERRORNEOUS_VALUE);
}catch(...)
{
s->memberFunction();
}
}
atleast this works. Now its upto me, how do i design my class.


Here you have traded a compile-time error for Undefined Behavior!
If the above code works, it is by accident - and it will fail
(or erase your HD etc) with a different compiler setting or some
other change of circumstances.
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #4
hi,
you may be correct, but what if i'm in a parent>child>sub-child
environment & want that after error in sub-child class, the child class
should not die/end.

makuchaku

Jul 22 '05 #5

makuchaku wrote:
hi,
you may be correct, but what if i'm in a parent>child>sub-child
environment & want that after error in sub-child class, the child class should not die/end.

makuchaku


Then you should do as I. Vecerina suggested. (Re)build the constructor
of Socket so that it will not throw (user defined exceptions that is)
and write an initializer function that is allowed to throw.

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: mag31 | last post by:
Is there any way to find out if a particular .net function will throw an exception without first generating the exception? I am using structured exception handling i.e. try catch finally blocks...
9
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned...
10
by: Jakob Bieling | last post by:
Hi, somehow the prejudice of exceptions being rather slow (compared to, ie. returning an error value and checking that) keeps sticking around .. at least around me. I guess this is also why I...
6
by: RepStat | last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
2
by: Jeff | last post by:
Hey ..NET 2.0 I'm developing an application (n-tier, data acces layer (DAL), bizlayer and presentation layer) If for example in the DAL a stored procedure call returns no rows then I want...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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...

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.