我正在从键盘上读取输入。输入应该与枚举类型中定义的元素之一匹配。这是枚举类型的示例:
type NameType is (Bob, Jamie, Steve);
Run Code Online (Sandbox Code Playgroud)
如果我收到的输入不是这3个之一,则ada引发IO异常。我如何处理此问题,使其仅显示“重试”消息而又不让程序停止?谢谢
创建Enumeration_IOfor 的实例Name_Type,例如Name_IO。在中loop,输入嵌套块以处理Data_Error出现的任何块。如果Name_IO.Get成功,exit将loop。
with Ada.IO_Exceptions;
with Ada.Text_IO;
procedure Ask is
type Name_Type is (Bob, Jamie, Steve);
package Name_IO is new Ada.Text_IO.Enumeration_IO (Name_Type);
begin
loop
declare
Name : Name_Type;
begin
Ada.Text_IO.Put("Enter a name: ");
Name_IO.Get(Name);
exit;
exception
when Ada.IO_Exceptions.Data_Error =>
Ada.Text_IO.Put_Line("Unrecognized name; try again.");
end;
end loop;
end Ask;
Run Code Online (Sandbox Code Playgroud)