Well, for starters I limit my
try … except on E: exception do … finally … end blocks to where the exception may raise. I
guess that's
database.open here. So it should be the first statement in the
try block. Also I gather all procedures, which are supposed to be called in the end, in the
finally block, even those, which don't make sense in the exception case (but don't harm, e.g. closing a file, which wasn't opened, because there was an exception). I don't know, whether that's particularly good, but it feels tidier.
function getAllFields(dbFile: string): TStringArray;
var
database: TDBF;
i: integer;
begin
database := TDBF.create(nil);
setLength(result, 0);
if not assigned(database) then
begin
exit;
end;
database.filePathFull := extractFileDir(dbFile);
database.tableName := extractFileName(dbFile);
try
begin
// may raise exception EMyHilariousException
database.open;
with database do
begin
setLength(result, fieldDefs.count);
for i := 0 to length(result) - 1 do
begin
// I haven't read the documentation, whether fieldDefs is zero-based
result[low(result) + i] := fieldDefs[low(fieldDefs) + i].name;
end;
end;
end;
except on E: EMyHilariousException do
begin
end;
finally
begin
database.close;
database.free;
end;
end;
end;
Maybe a bad habit of mine I adapted from the assembler world is to assign a default return value at first.
I also tend to put
everything into
begin..end; blocks, because especially
if you
nest try..except-blocks it can become pretty hairy.