Delphi提供了一個保留字finally
{分配資源}
try
{資源使用情況}
finally
{釋放資源}
end;
try…finally…end就形成了一個資源保護塊
對於(
var
APointer : Pointer ;
AInt
begin
ADiv :=
GetMem ( APointer
try
AInt :=
finally
FreeMem ( Apointer
end;
end;
下面的例子摘自(
procedure CopyFile(const FileName
var
CopyBuffer: Pointer;
TimeStamp
Source
Destination: TFileName;
const
ChunkSize: Longint =
begin
Destination := ExpandFileName(DestName)
if HasAttr(Destination
Destination := Destination +
TimeStamp := FileAge(FileName)
GetMem(CopyBuffer
try
Source := FileOpen(FileName
if Source <
raise EFOpenError
try
Dest := FileCreate(Destination)
if Dest <
raise EFCreateError
try
repeat
BytesCopied := FileRead(Source
if BytesCopied >
FileWrite(Dest
until BytesCopied < ChunkSize;
finally
FileClose(Dest)
end;
finally
FileClose(Source)
end;
finally
FreeMem(CopyBuffer
end;
end;
程序的具體解釋見 (
在異常保護的情況下
返回目錄
編輯推薦
Java程序設計培訓視頻教程
J
Visual C++音頻/視頻技術開發與實戰
Oracle索引技術
ORACLE
Java程序性能優化
C嵌入式編程設計模式
Android游戲開發實踐指南
[
From:http://tw.wingwit.com/Article/program/Delphi/201311/25194.html