On Wed, 13 Feb 2013, Giuliano Colla wrote:
Sven Barth ha scritto:
On 13.02.2013 10:11, Lukasz Sokol wrote:
On 13/02/2013 07:34, Michael Müller wrote:
I'm not sure if somebody else mentioned this already but I have the
feeling that Giuliano thinks that he has to decide to use try-finally OR
try-except but there are situations where you need both. One of the
criteria is if the object is local or global since for a global object it
is likely that you place the .Free (or for a global object better
FreeAndNil()) into a destructor or finalization section (which means that
you have to stop the program in case of an error and not continue showing
just a dialog) so you'll need only the try-except if you want to catch
the exception. In case of a local object you'll ALWAYS have the lines
Obj := Class.Create;
try
// do some code
finally
Obj.Free;
end;
otherwise you'll end up with memory leaks.
If you want to catch the exception in this situation you'll put
try-except extra around try-finally so you'll end up with
Obj := Class.Create;
try
try
// do some code
finally
Obj.Free;
end;
except
writeln('We have a problem);
halt the program or reraise the exception or raise a new one
end;
Regards
To developers:
How would a generalized/packed construct like
try
[code block]
finally
[code block]
except
[code block]
end;
or what about
try
[code block]
except
[code block]
finally
[code block]
end;
Python provides the following:
try
[code block]
except
[code block]
else
[code block]
finally
[code block]
end;
which can be used in any reasonable combination: just try..except, just
try..finally just try..except..else etc.
The except..else is a very useful construct, because it provides a path to a
code block to execute only if there were no previous errors.
This wouldn't break any existing applications, just add very useful features.
What fpc developers think about that?
"Else" is used in Except already to select between various classes:
try
..
except
on E : MyType do
begin
end
else
// exception is not MyType
end;
so that is a problem.
I see no problem in adding finally, if you define carefully when it is executed.
Michael.
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal