Assuming your class does nothing in it's constructor that can raise exceptions, there should be no need for this.
At this point if it fails, this will be due to some error elsewhere which wasn't caught, and you program will (and probably should) crash.
If your constructor can raise exceptions, and the constructor does not handle them, then MyVar will not be instantiated (will remain nil), and you will see the exception.
Mostly unhandled exceptions in a constructor will mean that something is catastrophically wrong, en even if you catch the exception, the program probably will not run properly.
See what happens when the constructor fails in the next program:
program test;
//{$mode objfpc}
//{$h+}
uses SysUtils;
type
{ TMyClass }
TMyClass = Class
constructor Create;
end;
var
MyVar: TMyClass;
{ TMyClass }
constructor TMyClass.Create;
begin
if (paramcount > 0) then raise exception.create('TMyVar construction exception');
end;
begin
Randomize;
try
try
MyVar := TMyClass.Create;
writeln('Addr(MyVar) = ',PtrInt(MyVar));
except
on E: Exception do writeln('Exception: ',E.Message,LineEnding,'Addr(MyVar) = ',PtrInt(MyVar));
end;
finally
if Assigned(MyVar) then
begin
write('Free MyVar ... ');
MyVar.Free;
writeln('Done.');
end
else
begin
writeln('MyVar = nil, no need to free it.');
end;
end;
end.
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
Addr(MyVar) = 23023104
Free MyVar ... Done.
Heap dump by heaptrc unit
53 memory blocks allocated : 986/1208
53 memory blocks freed : 986/1208
0 unfreed memory blocks : 0
True heap size : 196608 (112 used in System startup)
True free heap : 196496
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test 1
Exception: Random exception
Addr(MyVar) = 0
MyVar = nil, no need to free it.
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
Addr(MyVar) = 23023104
Free MyVar ... Done.
Heap dump by heaptrc unit
53 memory blocks allocated : 986/1208
53 memory blocks freed : 986/1208
0 unfreed memory blocks : 0
True heap size : 196608 (112 used in System startup)
True free heap : 196496
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test 1
Exception:TMyVar construction exception
Addr(MyVar) = 0
MyVar = nil, no need to free it.
Heap dump by heaptrc unit
56 memory blocks allocated : 1086/1312
56 memory blocks freed : 1086/1312
0 unfreed memory blocks : 0
True heap size : 229376 (128 used in System startup)
True free heap : 229248
As you see fpc cleans up the mess, there is no unfreed memory when the constructor fails.
Bart