Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Understanding Object Contructors


This question is answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Nov 30, 2017 7:33 AM Last Post By: G. Smith
G. Smith

Posts: 44
Registered: 5/3/03
Understanding Object Contructors  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 29, 2017 8:41 AM
Delphi in XE8

I think I once read (in the documentation a previous version of Delphi) that exceptions which occurred within an object constructor were automatically caught, memory released and a value on NIL was returned.

This doesn't seem to be the case in XE8. Can anyone point me to relevant documentation?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Understanding Object Contructors  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 29, 2017 9:51 AM   in response to: G. Smith in response to: G. Smith
G. Smith wrote:

I think I once read (in the documentation a previous version of
Delphi) that exceptions which occurred within an object constructor
were automatically caught, memory released and a value on NIL was
returned.

That is not entirely true. Try it for yourself. If a constructor
raises an exception, construction is aborted, the destructor is called
(so any memory already allocated can be freed), and the exception
continues propegating up the call stack normally until caught by a
matching 'except' statement.

--
Remy Lebeau (TeamB)
Alex Belo

Posts: 626
Registered: 10/8/06
Re: Understanding Object Contructors
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 30, 2017 6:41 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

If a constructor raises an exception, construction is aborted, the
destructor is called (so any memory already allocated can be freed)

so destructor must be ready to deal with partially constructed object.
But it's usually easy because constructor zeroizes object's memory as
very first action and destructor can blindly call Free for all internal
objects because Free(nil) does nothing.

NIL was returned

AFAIK Constructors never return (always raise exception) if
construction fails wherever.

--
Alex
G. Smith

Posts: 44
Registered: 5/3/03
Re: Understanding Object Contructors  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 30, 2017 7:33 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
/snipped/>
That is not entirely true. Try it for yourself. If a constructor
raises an exception, construction is aborted, the destructor is called
(so any memory already allocated can be freed), and the exception
continues propegating up the call stack normally until caught by a
matching 'except' statement.

OK, many thanks.

This is just what I am getting - I have to catch the exception. I've obviously mis-remembered.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02