Hello,
1 - What happens if my constructor raise an exception? Is my destructor automatically called?
2 - Are the class fields automatically initialized to Default(T) just like in Delphi?
Regards

Mailing lists / fpc-pascal / 2017-03
5 messages · 4 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hello,
1 - What happens if my constructor raise an exception? Is my destructor automatically called?
2 - Are the class fields automatically initialized to Default(T) just like in Delphi?
Regards
Hello, 1 - What happens if my constructor raise an exception? Is my destructor automatically called?
Yes.
2 - Are the class fields automatically initialized to Default(T) just like in Delphi?
They are all initialized. I'm not sure if Default(T) is always the same value. I guess yes.
Mattias
1 - What happens if my constructor raise an exception? Is my destructor automatically…
African Wild Dog wroteHello, 1 - What happens if my constructor raise an exception? Is my destructor automatically called?
Yes.
2 - Are the class fields automatically initialized to Default(T) just like in Delphi?
Yes. The're zeroed out when the memory for the class is allocated.
Just run the following program. You can add a field and print the contents in the destructor.
{$mode objfpc}
{$h+}
uses sysutils;
Type
TA = Class(TObject)
constructor create;
destructor destroy; override;
end;
Constructor TA.Create;
begin
Raise Exception.Create('aha');
end;
Destructor TA.Destroy;
begin
writeln('In destructor.');
end;
var
A : TA;
begin
A:=TA.Create;
end.2017-03-30 4:25 GMT-03:00 Michael Van Canneyt :
Thanks for the clarification.
I've issued # <http://bugs.freepascal.org/view.php?id=31619>0031619 <http://bugs.freepascal.org/view.php?id=31619> to add these details in the documentation.
Best regards
like
Keep in mind however that the second behavior can be changed by overriding TObject.NewInstance (the default implementation allocates the memory of the class and zeroes it).
Regards,
Sven