with Ada.Text_IO,Ada.Integer_Text_IO;
use Ada.Text_IO,Ada.Integer_Text_IO;
procedure Except2 is
procedure Divide_Loop(Index : in Integer)is
Divide_Result:Integer;
begin
Put("Index is");
Put(Index,3);
Put(" and the answer is ");
Divide_Result:=25/(4-Index);
Put(Divide_Result,4);
New_Line;
exception
when Constraint_Error=>Put(" divide by 0 error!");
Put_Line(" in loop 1. ");
end Divide_Loop;
procedure New_Divided_Loop(Index:in Integer)is
My_Own_Exception:exception;
Divide_Result:Integer;
begin
Put(" index is ");
Put(Index,3);
Put(" and the answer is ");
if Index =4 then
raise My_Own_Exception;
end if;
Divide_Result:=25/(4-Index);
Put(Divide_Result,4);
New_Line;
exception
when My_Own_Exception=>Put("Divide by zero");
Put_Line(" in loop 3 ");
when Constraint_Error=>Put_Line(" This should not happen! ");
Put_Line(" but it is included anyway!");
when others=>Put_Line("Som other exception happen!");
end New_Divided_Loop;
begin
Put_Line("Begin program here!");
for Count in 1..6 loop
Divide_Loop(Count);
end loop;
Put_Line(" End of first loop!");
for Count in 1..6 loop
declare
Divide_Result:Integer;
begin
Put("Count is");
Put(Count,3);
Put(" and the answer is ");
Divide_Result:=25/(4-Count);
Put(Divide_Result,4);
New_Line;
exception
when Constraint_Error=>Put(" divide by zero error ");
Put_Line("in loop 2");
end;
end loop;
Put_Line(" End of second loop ");
for Count in 1..6 loop
New_Divided_Loop(Count);
end loop;
Put_Line("End of the program");
end Except2;
080.Ada语言中的自定义异常处理
最新推荐文章于 2025-06-08 09:14:05 发布
本文深入探讨了Ada语言中异常处理的使用,通过多个实例展示了如何定义和捕获异常,包括预定义的Constraint_Error和自定义异常。文章详细解释了如何在循环中处理除以零错误,并提供了完整的代码示例。
136

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



