with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
procedure exception_example is
subtype Single_Digit_Int is Integer range 0..9;
Component : Single_Digit_Int;
begin
loop
begin -- start of block
Put_Line("Enter integer");
Get(Component);
exit;
exception
when Constraint_Error => -- start of handler
Put_Line("entered value is outside range. valid range is 0 to 9");
-- end of handler
end; -- end of block
end loop; -- end of loop
put("Component = ");put(Component);
end exception_example;
with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.IO_Exceptions;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
procedure Exceptions_Examples is
subtype Count_Range is Integer range 0 .. 10;
procedure Get_Count(Prompt : in String;
Count : out Count_Range) is
begin
loop -- Data validation loop
Put_Line(Prompt);
begin -- a block
Ada.Integer_Text_Io.Get(Count);
exit;
exception
when Constraint_Error =>
Put_Line("Value must be between 0 and 10");
when Ada.IO_Exceptions.Data_Error =>
Put_Line("Value must be a number");
Ada.Text_IO.Skip_Line;
end; -- the block
end loop;
exception
when Ada.IO_Exceptions.End_Error =>
Count := 0;
end Get_Count;
My_Count : Count_Range;
begin
Get_Count (Prompt => "Please enter the count",
Count => My_Count);
put("My_Count= ");Put(My_Count);
end Exceptions_Examples;