"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