TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Rick Duley <[log in to unmask]>
Reply To:
Rick Duley <[log in to unmask]>
Tue, 6 Nov 2001 22:48:50 -0500
text/plain (1302 bytes) , Fall_Off_The_End.adb (2835 bytes)
Hi teamers,

I was working on a demonstration exercise for students the other day and
gave myself one heck of a surprise.
The idea was that I could increment an integer variable past Integer'Last
and trap the ensuing uproar, doing something useful with it without crashing
the program.  Well, ...that was the idea :(

I am using GNAT 3.13p -- is this a problem with the compiler or have I
misunderstood the purpose of CONSTRAINT_ERROR all these years?  Do I have to
do a manual check for run-time errors like this or am I doomed to have my
jet plane turn upside down when the variable goes from positive to negative?

Keep the faith

Please: if you have not already done so, alter your addressbook settings for
me to
       [log in to unmask]
Rick Duley
Perth, Western Australia
tel: +61 040 910 6049                            /-_|\
                                                /     \
                                          perth *_.-._/
“Never be a pioneer.                                 v
    It's the early Christian
        that gets the fattest Lion."
            Saki (H. H. Munro (1870 - 1916))

Get your FREE download of MSN Explorer at

-- ada programming problem from -- rick duley -- edith cowan university -- perth, western australia -- [log in to unmask] -- -- explanations, suggestions, solutions please --         with Ada.Text_Io; use Ada.Text_Io;    with Ada.Exceptions; use Ada.Exceptions;    procedure Fall_Off_The_End is         --         -- in the PC implementation for which this program was         -- written and compiled with GNAT 3.13p the maximum         -- integer value (Integer'Last) is 2,147,486,647         -- this program starts with something close and increments         -- until the value is exceeded         -- the idea is to show that CONSTRAINT_ERROR is raised and         -- can be protected against without intefering with the         -- run of the program         --         -- oops!         --       Start : constant Integer := 2_147_483_600;       Log_File : File_Type;    begin -- Fall_Off_The_End       Create             (File => Log_File,              Mode => Out_File,              Name => "Fall_Off_The_End.txt");    Count_Loop:       for Count in 1 .. 100 loop          Protect_Block:                         declare                         -- this is where the fun should begin             Current : Integer := Start + Count;                         -- once the value of Count exceeds 47 this                         -- assignment should (IMHO) raise                         -- CONSTRAINT_ERROR and drive the point of                         -- control into the exception handler                         begin -- Protect_Block             Put_Line(Item => Integer'Image(Current));             Put_Line(File => Log_File,                      Item => Integer'Image(Current));             exception                when Error: others =>                -- write to log file                   Put_Line(File => Log_File,                            Item => Exception_Name(Error));                   Put_Line(File => Log_File,                            Item => Exception_Message(Error));                   Put_Line(File => Log_File,                            Item => "Resetting Current to"                            & Integer'Image(Integer'Last));                -- reset operating value                   Current := Integer'Last;                                         -- confirm in log file                                                 Put_Line(File => Log_File,                                                                         Item => Integer'Image(Current));                -- write to screen                   Put_Line(Item => Exception_Name(Error));                   Put_Line(Item => Exception_Message(Error));                   Put_Line(Item => "Current reset to"                            & Integer'Image(Integer'Last));                                                 Put_Line(Item => Integer'Image(Current));          end Protect_Block;       end loop Count_Loop;       Ada.Text_Io.Close             (File => Log_File);    end Fall_Off_The_End;