comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Extended Return Statement with exception
Date: Tue, 22 Nov 2005 18:06:18 -0600
Date: 2005-11-22T18:06:18-06:00	[thread overview]
Message-ID: <WsudnUaUocMZLB7enZ2dnUVZ_tidnZ2d@megapath.net> (raw)
In-Reply-To: 1132687889.831797.109510@o13g2000cwo.googlegroups.com

"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.







  parent reply	other threads:[~2005-11-23  0:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2005-11-23  2:49   ` Anh Vo
2005-11-23  9:24     ` Georg Bauhaus
2005-11-23 14:36       ` Anh Vo
replies disabled

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