Connecting Tech Pros Worldwide Help | Site Map

Exceptions creating Exceptional Problems!

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 11:31 PM
makuchaku
Guest
 
Posts: n/a
Default 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


  #2  
Old July 22nd, 2005, 11:31 PM
Ivan Vecerina
Guest
 
Posts: n/a
Default 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 22nd, 2005, 11:31 PM
makuchaku
Guest
 
Posts: n/a
Default 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 22nd, 2005, 11:31 PM
Ivan Vecerina
Guest
 
Posts: n/a
Default 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 22nd, 2005, 11:32 PM
makuchaku
Guest
 
Posts: n/a
Default 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 22nd, 2005, 11:33 PM
velthuijsen@hotmail.com
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.