unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
Eyearmeanerror = class(exception) //自定义的异常类
function transformyer(yearstr: string): string;
end;
function Eyearmeanerror.transformyer(yearstr: string): string;
begin
if strtoint(yearstr) > strtoint(copy(datetostr(date),0,4)) then
begin
showmessage('目前最晚只到' + #13 + '公元' + copy((datetostr(date)),0,4) + '年');
yearstr := copy(datetostr(date),0,4);
end
else
begin
if strtoint(yearstr) <= (strtoint(copy(datetostr(date),0,4)) - 1911) then
begin
showmessage('使用的是"公元"年');
yearstr := inttostr(strtoint(yearstr) + 1911);
end
else
yearstr := '';
end;
result := yearstr;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
thedate: tdatetime;
begin
try
thedate := strtodate(edit1.Text + '-' + edit2.Text + '-' + edit3.Text);
if (length(edit1.Text) < 4) or (strtoint(edit1.Text) > strtoint(copy(datetostr(date),0,4))) then
raise eyearmeanerror.Create('日期有误');
showmessage('你的生日是公元' + datetostr(thedate));
except
on e: econverterror do //捕捉内建的Econverterror异常
messagedlg('输入的不是数字' + #13 + '或日期不符合事实' + #13 + '年不可以大于9999!',mterror,[mbok],0);
on e: eyearmeanerror do //捕捉自定义的Eyearmeanerror异常
begin
messagedlg(e.Message,mterror,[mbok],0);
edit1.Text := e.transformyer(edit1.Text);
end;
else
raise; //产生其他异常
end;
end;
end.