newbie try throw and catch question 
Author Message
 newbie try throw and catch question

Hi,

I'm a reasonably experienced C++ programmer, but a complete
newbie to using try, throw and catch. If I can anyone can help
me clear these questions up, I'd be grateful.

If I do this :

void Fn()
{
   throw xc();

Quote:
}

void main()
{
   try
   {
      Fn();
   }
   catch (xc x)
   {
      ...
   }

Quote:
}

I presume the catch (xc x) causes a normal copy,
and that if xc::xc(xc & __xc) exists
that is what will do the work.

Are the steps followed :
1) source xc is constructed in Fn()
2) destination xc is constructed from source xc
3) source xc is destructed
4) stack is unwound to exception handler where
   destination xc exists ?

If so, does catch (xc & x) short circuit this ?

If it does, where is the exception object constructed -
on the stack for the routine throwing it, or in a target
area of the stack set aside for the exception handler ?

TVMIA,

Adam.

--
-------------------------------
Adam Benson

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 19 Mar 2002 03:00:00 GMT  
 newbie try throw and catch question
While the Standard doesn't specify where the exception object in catch
(xc & x) is stored, most implementation maintain a special exception
stack, on which exception are constructed (however, the Standard does
not allow the implementation to use free store memory for that purpose).
Passing an exception by reference is preferred to passing an exception
by value for several reasons: as you suggested, it short circuits the
process of passing the exception object to its handler (only a reference
is copied rather than a full-blown object). However, pass by reference
also ensures that derived exceptions are not sliced. Finally, it enables
you to modify the exception and then pass it on to another handler
(i.e., a re-throw) with the changes made to that object.

As for the steps of passing an exception by value: your steps describe
how the process works conceptually. In practice, many compilers can
optimize it and eliminate unnecessary construction and destruction of
temporary copies, while still maintaining the semantics that is
specified in the Standard.

Danny Kalev

"The ANSI/ISO C++ Professional Programmer's Handbook"
http://www.amazon.com/exec/obidos/ASIN/0789720221
http://www.devx.com/free/books/bookreview.asp?bookid=286

Quote:

> Hi,

> I'm a reasonably experienced C++ programmer, but a complete
> newbie to using try, throw and catch. If I can anyone can help
> me clear these questions up, I'd be grateful.

> If I do this :

> void Fn()
> {
>    throw xc();
> }

> void main()
> {
>    try
>    {
>       Fn();
>    }
>    catch (xc x)
>    {
>       ...
>    }
> }

> I presume the catch (xc x) causes a normal copy,
> and that if xc::xc(xc & __xc) exists
> that is what will do the work.

> Are the steps followed :
> 1) source xc is constructed in Fn()
> 2) destination xc is constructed from source xc
> 3) source xc is destructed
> 4) stack is unwound to exception handler where
>    destination xc exists ?

> If so, does catch (xc & x) short circuit this ?

> If it does, where is the exception object constructed -
> on the stack for the routine throwing it, or in a target
> area of the stack set aside for the exception handler ?

> TVMIA,

> Adam.

> --
> -------------------------------
> Adam Benson

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Thu, 21 Mar 2002 03:00:00 GMT  
 newbie try throw and catch question
Thanks, Danny,

that helps. I'll go with catch (xc & x). I was worried that this
approach might have some hidden gotchas in it since it doesn't seem
obvious where exactly the exception object actually lives.

The case I'm particuarly interested in trapping is out of memory errors
- throw new xc() is not recommended in that scenario !

- Adam.

-------------------------------
Adam Benson

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 22 Mar 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie question on try ... catch in ATL

2. newbie Simple Try Catch question

3. Does Finally execute try Catch throws an Exception?

4. try/catch/throw macros for C?

5. porting over try,catch, and throw

6. try, catch, throw...

7. Try, throw, catch ... doesn't

8. Why No Try-Throw-Catch Support in EVT?

9. TRY-CATCH vs. try-catch

10. try/catch VS TRY/CATCH

11. newbie Q: debugging try/catch

12. try...catch newbie, please help

 

 
Powered by phpBB® Forum Software