[comp.lang.ada] semantics of exception when exiting a procedure

daniel@mizar.docs.uu.se (Daniel Wengelin) (08/15/90)

Question:
What are the semantics when a procedure with out parameters is
exited by an exception. Can the out parameters be used, if they
have been assigned prior to the exception ?

Example>
procedure X(P1,P2,P3:out...)is
..
   P1:=....
   if ... raise E1;....
   P2:=....
   if .... raise E2;....
    P3:=...
end X;
.....
X(P1,P2,P3);
Do_Something_With(P1,P2,P3);
exception
   when E1=>Do_Something_With(P1);
    when E2 => Do_Something_With(P1,P2);
..

It would appear that the above code should work, but it does seem
a bit risky.

I would very much appreciate a LRM ref on this one !

/dw/

bhanafee@ADS.COM (Brian Hanafee) (08/16/90)

In article <1616@kuling.UUCP> daniel@mizar.docs.uu.se (Daniel Wengelin) writes:
>Question:
>What are the semantics when a procedure with out parameters is
>exited by an exception. Can the out parameters be used, if they
>have been assigned prior to the exception ?

[example deleted]

>I would very much appreciate a LRM ref on this one !
>
>/dw/

Try LRM 6.2/6, "...the effects are achieved by copy...after *normal*
completion of the subprogram body, if the mode is in out or out, the
value of the formal parameter is copied back into the associated
actual parameter."

This seems to say that the out parameters should *not* be used if
the procedure exited by exception.  6.2/12 and 6.4.1/7 give some
(vague) hints about why it might be specified this way.


Brian Hanafee
Advanced Decision Systems

jcallen@Encore.COM (Jerry Callen) (08/16/90)

In article <1616@kuling.UUCP> daniel@mizar.docs.uu.se (Daniel Wengelin) writes:
>Question:
>What are the semantics when a procedure with out parameters is
>exited by an exception. Can the out parameters be used, if they
>have been assigned prior to the exception ?
>
> [example deleted]

This whole area is described in something less than lucid detail in section
11.6 (Exceptions and Optimization). The example and comments in paragraphs
10 and 11 are helpful.

Basically, if the exception is raised in the body, you can depend only upon
initializations done in the declarations. In other words, the value of
out parameters is undefined.

-- Jerry Callen
   jcallen@encore.com

stluka@software.org (Fred Stluka) (08/16/90)

In article <1616@kuling.UUCP> daniel@mizar.docs.uu.se (Daniel Wengelin) writes:
> Question:
> What are the semantics when a procedure with out parameters is
> exited by an exception. Can the out parameters be used, if they
> have been assigned prior to the exception ?
> ...
> I would very much appreciate a LRM ref on this one !


6.2  Formal Parameter Modes

...

For a scalar parameter, the above effects are achieved  by  copy:   at  the
start  of  each  call, if the mode is in or in out, the value of the actual
parameter is copied into  the  associated  formal  parameter;   then  after
normal completion of the subprogram body, if the mode is in out or out, the
value  of  the  formal  parameter is copied back into the associated actual
parameter.  For a parameter whose type is an access type, copy-in  is  used
for all three modes, and copy-back for the modes in out and out.

For  a  parameter  whose  type  is  an  array,  record,  or  task  type, 
... an implementation may achieve
these effects by reference, that is, by arranging that  every  use  of  the
formal  parameter  (to  read or to update its value) be treated as a use of
the associated actual parameter, throughout the execution of the subprogram
call.  The language does not define which of these two mechanisms is to  be
adopted  for  parameter  passing,  nor  whether different calls to the same
subprogram are to use the same mechanism.  The execution of  a  program  is
erroneous  if  its  effect  depends  on  which mechanism is selected by the
implementation.

...

Notes:

For parameters of array and record types, the parameter passing rules  have
these consequences:

  -  If the execution of a subprogram  is  abandoned  as  a  result  of  an
     exception,  the  final value of an actual parameter of such a type can
     be either its value before the call or a value assigned to the  formal
     parameter during the execution of the subprogram.

--Fred

Fred Stluka                          Internet: stluka@software.org
Software Productivity Consortium     UUNET:    ...!uunet!software!stluka
2214 Rock Hill Rd, Herndon VA 22070