comp.lang.ada
 help / color / mirror / Atom feed
From: nytpu <alex@nytpu.com>
Subject: Variable value if exception is raised
Date: Sun, 20 Nov 2022 18:03:04 -0000 (UTC)	[thread overview]
Message-ID: <tldq4n$3i1p2$1@dont-email.me> (raw)

Hello everyone,

If an exception is *explicitly* raised during a variable assignment, what 
happens to the variable contents?  Are they in an undefined ("abnormal") 
state, or are the previous contents preserved?

For example:
```
with Ada.Text_IO;
procedure Test is
    function Always_Raises return Integer is
    begin
        raise Program_Error;
        return 1;
    end Always_Raises;
    
    I : Integer := 0;
begin
    -- insert a nested handler, because the ARM § 11.4 ¶ 3 *does*
    -- say that the currently executing body is "abnormally
    -- completed" (including finalizing everything) before
    -- entering the exception handler
    begin
        I := Always_Raises;
    exception
        when others => null;
    end;
    Ada.Text_IO.Put_Line(Integer'Image(I));
end;
```
What, if anything, will be printed?  (Disclaimer: I know the preexisting 
variable value will be preserved in GNAT specifically, but I'm asking if 
the standard guarantees that's the case)

I read through the ARM 2012 § 11 and § 5.2, as well as skimming through 
everything related to “assignment” and “exceptions” in the ARM index;
and didn't see much relating to this.  All I saw is this:
> When an exception occurrence is raised by the execution of a given 
> construct, the rest of the execution of that construct is abandoned
— ARM 2012 § 11.4 ¶ 3
Which I guess implicitly protects variable values since assigning to a 
variable is performed after evaluating the right hand side, but still not 
necessarily a clear answer.

I did see in § 13.9.1 that language-defined validity checks (e.g. bounds 
checks) failing or calling `abort` in a task during an assignment will 
cause the variable to enter an "abnormal" (i.e. invalid) state, but that 
doesn't cover user-raised exceptions.

-- 
~nytpu

             reply	other threads:[~2022-11-20 18:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-20 18:03 nytpu [this message]
2022-11-20 19:00 ` Variable value if exception is raised Jeffrey R.Carter
replies disabled

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