Figure 6.
Range checking example.
------------------------------------------------------
-- Ada knows that objects of type Celsius can
-- never be colder than absolute zero, and will
-- raise CONSTRAINT_ERROR if an attempt is made
-- to assign a value that is out of range.
with FLOAT_UNITS;
with TEXT_IO; use TEXT_IO;
procedure Range_Checking_Example is
package TEMPERATURE is new FLOAT_UNITS
(Float_type => float,
MIN => -273.16,
MAX => 32000.0,
Integer_type => integer);
type Celsius is new TEMPERATURE.Units;
LAB_TEMPERATURE : Celsius;
begin
begin
LAB_TEMPERATURE := Type_Convert(25.0);
put_line("Ada will let you assign"
& " reasonable values.");
exception
when others =>
put_line("Ada FAILED to assign a correct value.");
end;
begin
LAB_TEMPERATURE := Type_Convert(-300.0);
put_line("FAILED to detect too cold.");
exception
when CONSTRAINT_ERROR =>
put_line("Ada won't let you assign values"
& " that are too cold.");
end;
begin
LAB_TEMPERATURE := Type_Convert(32100.0);
put_line("FAILED to detect too hot.");
exception
when CONSTRAINT_ERROR =>
put_line("Ada won't let you assign values"
& " that are too hot.");
end;
end Range_Checking_Example;
-------- When you run it, here's what you get: --------
C:>RANGE_CHECKING_EXAMPLE
Ada will let you assign reasonable values.
Ada won't let you assign values that are too cold.
Ada won't let you assign values that are too hot.