with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Exceptions;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Occur is
Except_ID : Ada.Exceptions.EXCEPTION_OCCURRENCE;
procedure Divide_Loop is
Divide_Result : INTEGER;
begin
for Index in 1..8 loop
Put("Index is");
Put(Index, 3);
Put(" and the answer is");
Divide_Result := 25 / (4 - Index);
Put(Divide_Result, 3);
New_Line;
end loop;
exception
when Except_ID: Constraint_Error =>
Put_Line(" Divide by zero error.");
New_Line;
Put_Line(" Exception name:");
Put_Line(Ada.Exceptions.Exception_Name(Except_ID));
New_Line;
Put_Line(" Exception information:");
Put_Line(Ada.Exceptions.Exception_Information(Except_ID));
New_Line;
when Except_ID: Storage_Error =>
Put_Line(" Storage error detected.");
Put_Line(Ada.Exceptions.Exception_Information(Except_ID));
New_Line;
when Except_ID: others =>
Put_Line(" Unknown exception detected.");
Put_Line(Ada.Exceptions.Exception_Information(Except_ID));
New_Line;
end Divide_Loop;
begin
Put_Line("Begin program here.");
Divide_Loop;
Put_Line("End of program.");
end Occur;
085.使用Ada内置的异常库
最新推荐文章于 2025-07-10 16:52:41 发布
本文深入探讨了Ada语言中异常处理的使用方法,通过一个具体的除法运算循环程序示例,展示了如何捕获并处理诸如除以零错误、存储错误等异常情况。文章详细解释了Ada.exceptions包的使用,以及如何通过EXCEPTION_OCCURRENCE记录类型来获取异常信息。
1459

被折叠的 条评论
为什么被折叠?



