口令檢查是用戶在PassWord中輸入口令並按下回車鍵後開始的
procedure TForm
const
CurrentPassWord =
begin
if Key = #
begin
try
if PassWord
raise EInvalidPassWord
Label
InputEdit
InputEdit
PassWord
Label
except
on EInvalidPassWord do
begin
PassWord
raise;
end;
end;
Key:=#
end;
end;
同樣
procedure TForm
var
Res: Real;
Code: Integer;
begin
if Key = #
begin
try
val(InputEdit
if Code <>
raise EInValidInput
if (Res >
raise EInValidInput
MessageDlg(
Key := #
except
on E:EInValidInput do
begin
InputEdit
MessageDlg(E
end;
end;
end;
end;
由於異常響應後即被清除
以上所舉的是一個非常簡單的例子
利用異常處理機制不僅能使程序更加健壯
比如為了防止零作除數
function Calcu(x
begin
try
Result := x/a+y/b+z/c ;
except
on EDivByZero do
Result :=
end;
end;
在(
procedure TRecFileForm
begin
if OpenDialog
FileName := OpenDialog
else
exit;
AssignFile(MethodFile
try
Reset(MethodFile)
FileOpened := True;
except
on EInOutError do
begin
try
if FileExists(FileName) = False then
begin
ReWrite(MethodFile)
FileOpened := True;
end
else
begin
FileOpened := False;
MessageDlg(
end;
except
on EInOutError do
begin
FileOpened := False;
MessageDlg(
end;
end;
end;
end;
if FileOpened = False then exit;
Count := FileSize(MethodFile)
if Count >
ChangeGrid;
RecFileForm
NewButton
OpenButton
CloseButton
end;
總之
[
From:http://tw.wingwit.com/Article/program/Delphi/201311/25190.html