084.关于异常的一些附加内容--示例程序

stuff.ads

package Stuff is
   Funny_Add_Error : exception;
   procedure Add_One(Number : in out INTEGER);
   function Subtract_One(Number : INTEGER) return INTEGER;
end Stuff;

stuff.adb

with Ada.Text_IO;
use Ada.Text_IO;

package body Stuff is

   procedure Add_One(Number : in out INTEGER) is
   begin
      Number := Number + 1;
   exception
      when Funny_Add_Error => Put_Line("Funny add error raised");
      when Constraint_Error => Put_Line("Constraint error raised");
   end Add_One;

   function Subtract_One(Number : INTEGER) return INTEGER is
      Funny_Subtract_Error : exception;
   begin
      raise Funny_Subtract_Error;
      return Number - 1;
   exception
      when Funny_Add_Error =>
                         Put_Line("Funny add error raised");
                         raise;
      when Funny_Subtract_Error =>
                         Put_Line("Funny subtract error raised");
                         raise;
   end Subtract_One;

begin
   null;
exception
-- Numeric_Error is obsolete in Ada 95.  It is another name for
--  the exception Constraint_Error.
-- when Numeric_Error =>
--                  Put_Line("Numeric error during elaboration");
   when Constraint_Error =>
                 Put_Line("Constraint_Error during elaboration");
   when Funny_Add_Error =>
                  Put_Line("Funny Add error during elaboration");
-- when Funny_Subtract_Error =>
--           Put_Line("Funny subtract error during elaboration");
end Stuff;

main.adb

with Ada.Text_IO, Stuff;
use Ada.Text_IO, Stuff;

procedure Except5 is
   Index : INTEGER := 5;
   Add_Error : exception renames Stuff.Funny_Add_Error;
begin
   Add_One(Index);
   Index := Subtract_One(Index);
exception
   when Numeric_Error | Constraint_Error =>
             Put_Line("Numeric error or constraint error raised.");
   when Add_Error =>
             Put_Line("Addition error detected");
   when others =>
             Put_Line("An unknown exception raised");
             raise;      -- Raise it again for the operating system
end Except5;




-- Result of execution

-- Funny subtract error raised
-- An unknown exception raised
-- Unhandled exception; funny_subtract_error

-- (Note, The last line above will be different for each compiler,
--        but will say something about an unhandled exception.  It
--        will probably output about 10 lines of text.)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值