Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə189/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   185   186   187   188   189   190   191   192   ...   294
DelphiLanguageGuide

Re-raising Exceptions
When the reserved word raise occurs in an exception block without an object reference following it, it raises whatever
exception is handled by the block. This allows an exception handler to respond to an error in a limited way and then
re-raise the exception. Re-raising is useful when a procedure or function has to clean up after an exception occurs
but cannot fully handle the exception.
For example, the 
GetFileList
 function allocates a 
TStringList
 object and fills it with file names matching a
specified search path:
function GetFileList(const Path: string): TStringList; 
var 
  I: Integer; 
  SearchRec: TSearchRec; 
begin 
  Result := TStringList.Create; 
  try 
    I := FindFirst(Path, 0, SearchRec); 
    while I = 0 do 
      begin 
          Result.Add(SearchRec.Name); 
          I := FindNext(SearchRec); 
      end; 
  except 
      Result.Free; 
      raise; 
  end; 
end;
GetFileList
 creates a 
TStringList
 object, then uses the 
FindFirst
 and 
FindNext
 functions (defined in
SysUtils
) to initialize it. If the initialization fails - for example because the search path is invalid, or because there
is not enough memory to fill in the string list - 
GetFileList
 needs to dispose of the new string list, since the caller
does not yet know of its existence. For this reason, initialization of the string list is performed in a 
try...except
statement. If an exception occurs, the statement's exception block disposes of the string list, then re-raises the
exception.
164



Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   185   186   187   188   189   190   191   192   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin