谁有自定义异常的实例代码?多谢,一经试用成功,就给分。

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
    end;
    var
    Form1: TForm1;type
      eageexception=class(exception)
    public
      data:integer;
      constructor create(const msg:string;age:integer);
    end;implementation{$R *.dfm}constructor eageexception.create;
    begin
      inherited create(msg);
      data:=age;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
    age:integer;
    begin
      try
      age:=strtoint(edit1.Text );
      if (age<=0) or (age>108) then
      raise eageexception.create('输入的年龄无效',age);
      except
      on  ex:eageexception do
      showmessage('年龄为'+inttostr(ex.data )+',视为无效');
      on econverterror do
      showmessage('年龄应为整数');
      end;
    end;end.
      

  2.   

    感谢ctd(☆常想一二☆),给分!