Exceptions creating Exceptional Problems!

M

makuchaku

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/01/exceptions-creating-exceptional.html
Do reply asap...

Thanks,
makuchaku
 
I

Ivan Vecerina

makuchaku said:
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/01/exceptions-creating-exceptional.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
 
M

makuchaku

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
 
I

Ivan Vecerina

makuchaku said:
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
 
M

makuchaku

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
 
V

velthuijsen

makuchaku said:
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.
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top