comp.lang.ada
 help / color / mirror / Atom feed
* Extended Return Statement with exception
@ 2005-11-22 19:31 Anh Vo
  2005-11-22 21:46 ` Pascal Obry
  2005-11-23  0:06 ` Randy Brukardt
  0 siblings, 2 replies; 7+ messages in thread
From: Anh Vo @ 2005-11-22 19:31 UTC (permalink / raw)


I have read the Extended Return Statement in Ada 2005 RM and Rationale.
However, I am still not sure how it works. In other word, what will be
returned when an exception occurs and handled the the exception handler
within this extended return statement. Consider the following notional
code, what exactly will be returned?

type T is limited ...
...

function Make (...) return T is
begin
   ...
   return R : T do
      ...
      ...   -- Constraint_Error is raised here for example
   exception
      ... handle Constraint_Error here.
   end return;
end Make;

AV




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

* Re: Extended Return Statement with exception
  2005-11-22 19:31 Extended Return Statement with exception Anh Vo
@ 2005-11-22 21:46 ` Pascal Obry
  2005-11-22 22:27   ` Anh Vo
  2005-11-23  0:06 ` Randy Brukardt
  1 sibling, 1 reply; 7+ messages in thread
From: Pascal Obry @ 2005-11-22 21:46 UTC (permalink / raw)
  To: Anh Vo

Anh,

> I have read the Extended Return Statement in Ada 2005 RM and Rationale.
> However, I am still not sure how it works. In other word, what will be
> returned when an exception occurs and handled the the exception handler
> within this extended return statement. Consider the following notional
> code, what exactly will be returned?

Well what will be returned in this case:

   function Make (...) return T is
      R : T;
   begin
      ...
      return R;
   exception
      ... handle Constraint_Error here.
   end Make;

I think this is equivalent to what will happen with your example with a
structured return statement. Of course I should not think but have a
look at the RM but I'm too lazy tonight ;)

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595



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

* Re: Extended Return Statement with exception
  2005-11-22 21:46 ` Pascal Obry
@ 2005-11-22 22:27   ` Anh Vo
  0 siblings, 0 replies; 7+ messages in thread
From: Anh Vo @ 2005-11-22 22:27 UTC (permalink / raw)


Hi Pascal,

I believe that there missing return statement in your exception
handler. I assume that this return statement is "return R;". Otherwise,
the compiler will not happy :-). Note that R may not be fully defined
due to the exception above.

AV




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

* Re: Extended Return Statement with exception
  2005-11-22 19:31 Extended Return Statement with exception Anh Vo
  2005-11-22 21:46 ` Pascal Obry
@ 2005-11-23  0:06 ` Randy Brukardt
  2005-11-23  2:49   ` Anh Vo
  1 sibling, 1 reply; 7+ messages in thread
From: Randy Brukardt @ 2005-11-23  0:06 UTC (permalink / raw)


"Anh Vo" <anhvofrcaus@gmail.com> wrote in message
news:1132687889.831797.109510@o13g2000cwo.googlegroups.com...
> I have read the Extended Return Statement in Ada 2005 RM and Rationale.
> However, I am still not sure how it works. In other word, what will be
> returned when an exception occurs and handled the the exception handler
> within this extended return statement. Consider the following notional
> code, what exactly will be returned?
>
> type T is limited ...
> ...
>
> function Make (...) return T is
> begin
>    ...
>    return R : T do
>       ...
>       ...   -- Constraint_Error is raised here for example
>    exception
>       when Constraint_Error => null; -- handle Constraint_Error here.
>    end return;
> end Make;

(I changed your example a bit to be clear.)

The answer is that if the extended return completes normally, it's object is
returned. So, in this example, R is returned. You could complain that the
object isn't properly initialized, but of course if you put a handler here,
you ought to set up the object correctly (that is, if you handle the
exception, you really need to handle it, not just ignore it).

If you don't want to return R, you need to handle the exception outside of
the return statement:

function Make (...) return T is
begin
   ...
   begin
      return R : T do
      ...
      ...   -- Constraint_Error is raised here for example
      end return;
   exception
      when Constraint_Error =>
         return Some_Default_Value; -- handle Constraint_Error here.
   end;
end Make;

There are some interesting interactions between exceptions, finalization,
and return statements (imagine T has tasks or controlled components to make
your head hurt), but these shouldn't be a concern for anyone other than
implementers.

                                Randy.







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

* Re: Extended Return Statement with exception
  2005-11-23  0:06 ` Randy Brukardt
@ 2005-11-23  2:49   ` Anh Vo
  2005-11-23  9:24     ` Georg Bauhaus
  0 siblings, 1 reply; 7+ messages in thread
From: Anh Vo @ 2005-11-23  2:49 UTC (permalink / raw)


I get it. Thanks. There is one more question though. It seems to me
that an exception handler within an extended return statement do not
have much value because it can not contain a nested return statement?

AV




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

* Re: Extended Return Statement with exception
  2005-11-23  2:49   ` Anh Vo
@ 2005-11-23  9:24     ` Georg Bauhaus
  2005-11-23 14:36       ` Anh Vo
  0 siblings, 1 reply; 7+ messages in thread
From: Georg Bauhaus @ 2005-11-23  9:24 UTC (permalink / raw)


Anh Vo wrote:
> I get it. Thanks. There is one more question though. It seems to me
> that an exception handler within an extended return statement do not
> have much value because it can not contain a nested return statement?

Can't you write

function Foo return Character is
begin
   return C: Character do
     C := Fail;
   exception
      when Some_Error => C := Default_Char;
   end return;
end Foo;



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

* Re: Extended Return Statement with exception
  2005-11-23  9:24     ` Georg Bauhaus
@ 2005-11-23 14:36       ` Anh Vo
  0 siblings, 0 replies; 7+ messages in thread
From: Anh Vo @ 2005-11-23 14:36 UTC (permalink / raw)


Yes, that is exactly what I was thinking of. In this case, exception
handler within an extended return statement does have value. Thanks
Georg

AV

Georg Bauhaus wrote:
> Anh Vo wrote:
> > I get it. Thanks. There is one more question though. It seems to me
> > that an exception handler within an extended return statement do not
> > have much value because it can not contain a nested return statement?
>
> Can't you write
>
> function Foo return Character is
> begin
>    return C: Character do
>      C := Fail;
>    exception
>       when Some_Error => C := Default_Char;
>    end return;
> end Foo;




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

end of thread, other threads:[~2005-11-23 14:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-22 19:31 Extended Return Statement with exception Anh Vo
2005-11-22 21:46 ` Pascal Obry
2005-11-22 22:27   ` Anh Vo
2005-11-23  0:06 ` Randy Brukardt
2005-11-23  2:49   ` Anh Vo
2005-11-23  9:24     ` Georg Bauhaus
2005-11-23 14:36       ` Anh Vo

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