Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* TRY_CATCH()
@ 2005-01-13  0:09 Andrew Cagney
  2005-01-13 19:48 ` TRY_CATCH() Kris Warkentin
  2005-01-14 19:29 ` TRY_CATCH() Frank Ch. Eigler
  0 siblings, 2 replies; 3+ messages in thread
From: Andrew Cagney @ 2005-01-13  0:09 UTC (permalink / raw)
  To: gdb

Hello,

The exceptions rewrite opens up a key possibility.  It's now possible to 
define a macro such that:

   struct args
   {
     ...
   };

   do_func (void *data)
   {
     struct args *args = data;
     ... do stuff ...;
   }

   func (...)
   {
     struct args args;
     args... = ...;
     struct exception e = catch_exception (..., func, &args, ...)
     switch (e.reason)
       ...
   }

can be replaced with the more compact:

   func (...)
   {
     volatile struct exception e;
     TRY_CATCH (e)
       {
         ... do stuff ...;
       }
     switch (e.reason)
       ...
   }

Yes I know macros a bad, m'kay.  However, this offers a simple way to 
rewrite the code using something more like a real programming languages 
try/catch.

Andrew

PS: I recall this being proposed years ago, but the mechanism proposed 
at the time had a more cumbersom syntax, from memory something like:

   TRY_BEGIN
     ... do stuff ...
   TRY_END;

comments?
Andrew


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: TRY_CATCH()
  2005-01-13  0:09 TRY_CATCH() Andrew Cagney
@ 2005-01-13 19:48 ` Kris Warkentin
  2005-01-14 19:29 ` TRY_CATCH() Frank Ch. Eigler
  1 sibling, 0 replies; 3+ messages in thread
From: Kris Warkentin @ 2005-01-13 19:48 UTC (permalink / raw)
  To: Andrew Cagney; +Cc: gdb

Andrew Cagney wrote:

>PS: I recall this being proposed years ago, but the mechanism proposed 
>at the time had a more cumbersom syntax, from memory something like:
>
>   TRY_BEGIN
>     ... do stuff ...
>   TRY_END;
>  
>

I saw something similar to this that worked really well but was not 
quite as generic.  Basically you would have to add a 'TryStack' member 
to whatever data structure you were planning to capture exceptions on.  
The TryStack was a stack of jmp_bufs.  Then you'd do something like

if((error = TRY(mystruct)) == 0){
    do_stuff(mystruct);
    TRY_END(mystruct->try_stack);
}
else{
    CATCH(mystruct, error);
    handle_error();
}

The TRY would push a new jmp_buf onto the TryStack and the TRY_END and 
CATCH would pop one.  The various macros would be like #define TRY(foo) 
is try_push(foo->try_stack) and so on.  It's actually quite clever:  If 
something THROWs, it also pops but CATCH only pops if the error value is 
zero so that you can have multiple tries and catches on a single block.

Anyway, interesting stuff.

cheers,

Kris


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: TRY_CATCH()
  2005-01-13  0:09 TRY_CATCH() Andrew Cagney
  2005-01-13 19:48 ` TRY_CATCH() Kris Warkentin
@ 2005-01-14 19:29 ` Frank Ch. Eigler
  1 sibling, 0 replies; 3+ messages in thread
From: Frank Ch. Eigler @ 2005-01-14 19:29 UTC (permalink / raw)
  To: Andrew Cagney; +Cc: gdb


cagney wrote:

> [...]
>    func (...)
>    {
>      volatile struct exception e;
>      TRY_CATCH (e)
>        {
>          ... do stuff ...;
>        }
>      switch (e.reason)
>        ...
>    }
> [...]
> comments?

At this rate, by around 2009, gdb will have reimplemented 
the remainder of C++.

- FChE


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-01-14 19:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-13  0:09 TRY_CATCH() Andrew Cagney
2005-01-13 19:48 ` TRY_CATCH() Kris Warkentin
2005-01-14 19:29 ` TRY_CATCH() Frank Ch. Eigler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox