Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The scope of __finally (C++ Builder 2010)

47 views
Skip to first unread message

A

unread,
Nov 20, 2014, 3:33:09 PM11/20/14
to
In this particular example:

int TForm1::TestOfFinally()
{
int i = 111;
std::vector<int> aa;

aa.push_back(123);

try
{
return i;
}
__finally
{
Application->MessageBox(IntToStr((int)aa.size()).c_str(), L"Info",
MB_OK | MB_ICONINFORMATION);
}
}

aa.size() is 0 as it goes out of scope after return i; in try part of the
block.

Isn't __finally supposed to keep variables in scope until it finishes?

If return i; is removed (or moved after the __finally block), the message
box shows 1 instead of 0 as above for vector size.

The above example is in C++ Builder 2010.


Paavo Helde

unread,
Nov 20, 2014, 5:39:54 PM11/20/14
to
"A" <a@a.a> wrote in news:m4lj5n$m3i$1...@gregory.bnet.hr:

> In this particular example:
>
> try
> {
> return i;
> }
> __finally

The C++ standard does not contain word "__finally". The identifiers
containing double underscores are reserved for implementations, so this is
most probably some kind of extension provided by your particular C++
implementation (and if I am not mistaken, it is probably meant to be used
together with __try, not try). Maybe it would work better with __try, who
knows.

The "finally" thing is used as a poor substitute for RAII in languages
which are lacking it. In C++ we have RAII and proper destructions (and
things like ScopeGuard if one is too lazy to write a helper class with a
destructor), so it baffles me why somebody would ever need functionality of
"finally" in C++, it is just inferior in every sense IMO.

Cheers
Paavo

A

unread,
Nov 20, 2014, 8:07:43 PM11/20/14
to
"Paavo Helde" <myfir...@osa.pri.ee> wrote in message
news:XnsA3EC6B464CA4my...@216.196.109.131...
> The C++ standard does not contain word "__finally". The identifiers
> containing double underscores are reserved for implementations, so this is
> most probably some kind of extension provided by your particular C++
> implementation (and if I am not mistaken, it is probably meant to be used
> together with __try, not try). Maybe it would work better with __try, who
> knows.

Yes, it is not a standard keyword. But the try - __finally is valid in C++
Builder
and it is meant to be used like this.

> The "finally" thing is used as a poor substitute for RAII in languages
> which are lacking it. In C++ we have RAII and proper destructions (and
> things like ScopeGuard if one is too lazy to write a helper class with a
> destructor), so it baffles me why somebody would ever need functionality
> of
> "finally" in C++, it is just inferior in every sense IMO.

I do use scoped variables all the time but in this particular code I require
this kind of construct. Anyway, I don't want to get into too deep discussion
what is right and what is not, just the reason why is the vector destroyed
in the part when it reaches __finally?


Richard

unread,
Nov 20, 2014, 9:02:17 PM11/20/14
to
[Please do not mail me a copy of your followup]

"A" <a@a.a> spake the secret code
<m4lj5n$m3i$1...@gregory.bnet.hr> thusly:

>In this particular example:
>
> 1 int TForm1::TestOfFinally()
> 2 {
> 3 int i = 111;
> 4 std::vector<int> aa;
> 5
> 6 aa.push_back(123);
> 7
> 8 try
> 9 {
> 10 return i;
> 11 }
> 12 __finally
> 13 {
> 14 Application->MessageBox(IntToStr((int)aa.size()).c_str(), L"Info",
> MB_OK | MB_ICONINFORMATION);
> 15 }
> 16 }
>
>aa.size() is 0 as it goes out of scope after return i; in try part of the
>block.

If you get 0 for aa.size() from this code in C++ Builder, then it's a
bug in C++ Builder.

aa doesn't go out of scope until line 16.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Paavo Helde

unread,
Nov 21, 2014, 12:15:23 AM11/21/14
to
"A" <a@a.a> wrote in news:m4m389$req$1...@gregory.bnet.hr:

> Anyway, I don't want to get into too
> deep discussion what is right and what is not, just the reason why is
> the vector destroyed in the part when it reaches __finally?

Well, OK, just be aware you might not get best answers here because this is
the wrong group.

Cheers
Paavo

David Brown

unread,
Nov 21, 2014, 3:03:44 AM11/21/14
to
I have only briefly used C++ Builder, and that was many years ago. But
I believe I can give you a more complete reasoning for the __finally
extension here. C++ Builder was made by Borland to combine their C/C++
tools with Delphi, which uses their extended "Object Pascal". Object
Pascal does not support RAII - its constructors and destructors are
called manually rather than automatically, and thus it needs a "finally"
clause for its exceptions. C++ Builder uses much of the Delphi library,
and many of its users come from Delphi backgrounds - so Borland added a
__finally keyword for compatibility here.


A

unread,
Nov 22, 2014, 10:25:23 AM11/22/14
to
Thank you all for providing valuable insight. I will avoid using return in
the try block as it seems. Found more detailed answer here for those
interested:
https://forums.embarcadero.com/thread.jspa?threadID=110377


0 new messages