Connecting Tech Pros Worldwide Help | Site Map

Exceptions creating Exceptional Problems!

  #1  
Old July 23rd, 2005, 12:31 AM
makuchaku
Guest
 
Posts: n/a
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

  #2  
Old July 23rd, 2005, 12:31 AM
Ivan Vecerina
Guest
 
Posts: n/a

re: Exceptions creating Exceptional Problems!


"makuchaku" <mayank.gnu@gmail.com> wrote in message
news:1105226668.209678.33520@c13g2000cwb.googlegro ups.com...[color=blue]
> 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...[/color]
The code was short enough that you could have posted it here:
[color=blue]
> #include<...>
> void main()
> {
> try
> {
> Socket object();[/color]
The above is a function declaration. I guess you meant:
Socket object;[color=blue]
> object.do_something_else();
> }catch(SockException err)
> {
> err.getCode(); //works
> err.getMessage(); //works
> //object.cannot_access_this(); <----- this is troublesome![/color]
For that to work, 'object' need to be declared outside of/prior to
the try block:
Socket object;
try {
.....[color=blue]
> }
> }[/color]

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


  #3  
Old July 23rd, 2005, 12:31 AM
makuchaku
Guest
 
Posts: n/a

re: Exceptions creating Exceptional Problems!


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

  #4  
Old July 23rd, 2005, 12:31 AM
Ivan Vecerina
Guest
 
Posts: n/a

re: Exceptions creating Exceptional Problems!


"makuchaku" <mayank.gnu@gmail.com> wrote in message
news:1105231766.000307.35180@c13g2000cwb.googlegro ups.com...[color=blue]
> 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.[/color]

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


  #5  
Old July 23rd, 2005, 12:32 AM
makuchaku
Guest
 
Posts: n/a

re: Exceptions creating Exceptional Problems!


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

  #6  
Old July 23rd, 2005, 12:33 AM
velthuijsen@hotmail.com
Guest
 
Posts: n/a

re: Exceptions creating Exceptional Problems!



makuchaku wrote:[color=blue]
> 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[/color]
class[color=blue]
> should not die/end.
>
> makuchaku[/color]

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.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
.Net Exceptions mag31 answers 21 November 22nd, 2005 02:38 PM
the c# return statement John Bailo answers 94 November 22nd, 2005 11:17 AM
.Net Exceptions mag31 answers 24 July 21st, 2005 06:47 PM
the c# return statement John Bailo answers 94 July 21st, 2005 03:31 PM