Am I right in thinking a return value is carried over into a __finally statement?

D

deanbrown3d

I mean, is this correct?

try
{
Screen->Cursor = crHourglass;
Do something bad
return false;
else
return true;
}
__finally
{
Screen->Cursor = crDefault;
// No need for return statement here.
}

The question is, will the return value (true or false) be returned
correctly as shown, in the __finally statement?

THX!

Dean
 
V

Victor Bazarov

I mean, is this correct?

try
{
Screen->Cursor = crHourglass;
Do something bad
return false;
else
return true;
}
__finally

This is not C++. If this is another language, look up the newsgroup
that covers that language. If this is a compiler extension, please
post to the newsgroup dedicated to that compiler.
{
Screen->Cursor = crDefault;
// No need for return statement here.
}

The question is, will the return value (true or false) be returned
correctly as shown, in the __finally statement?

No way to tell based on the C++ language specification because it does
not have the "__finally" thing.

V
 
D

Dietmar Kuehl

I mean, is this correct?

Correct according to what? It is clearly not covered by the C++ as
a user is not allowed to even utter certain names, e.g. those
starting with an underscore followed by an underscore or an
uppercase letter. A particular C++ implementation may, as an
extension, grant the user the right to use some of these names and
to associate whatever it desires with these names.

Thus, you should direct your article to an appropriate environment
specific forum.

The C++ way of doing what you want uses an object whose destructor
does the clean-up. You may want to look for RAII.
 
D

deanbrown3d

Yeah you're right, its a borland keyword extension. I'll repost there.

Thanks

Dean
 
V

Victor Bazarov

It only gets 1 hit a month though:(

Well, there are many 'borland.public.cpp.*' newsgroups, perhaps look for
another one, or ask in 'borland.public.cppbuilder.language'...
 
C

Calum Grant

I mean, is this correct?

try
{
Screen->Cursor = crHourglass;
Do something bad
return false;
else
return true;
}
__finally
{
Screen->Cursor = crDefault;
// No need for return statement here.
}

The question is, will the return value (true or false) be returned
correctly as shown, in the __finally statement?

That's not C++. Assuming you mean catch(xxx), then no, the exception is
thrown before the return statement, so the return is never reached and
it is not used at all.

Hmm, did you have

#define __finally catch(...)

that's non-standard and I wouldn't recommend it.
 
D

deanbrown3d

I think it comes originally from Delphi and Pascal use, you know that
builder from borland can compile pascal and C++. And __finally is VERY
useful in Pascal. You can return a value, and its carries it on for you
even after processing the __finally section. Great stuff!
 
M

Matthias Kaeppler

I think it comes originally from Delphi and Pascal use, you know that
builder from borland can compile pascal and C++. And __finally is VERY
useful in Pascal. You can return a value, and its carries it on for you
even after processing the __finally section. Great stuff!

Java also has a finally construct. IIRC it makes sure that the specific
block is /always/ executed, regardless what happens. Would be nice to
have that in C++ too, for cleanup purposes for example.
 
V

Victor Bazarov

Matthias said:
Java also has a finally construct. IIRC it makes sure that the specific
block is /always/ executed, regardless what happens. Would be nice to
have that in C++ too, for cleanup purposes for example.

I think the destructors of local objects exist (and are called) exactly
for that purpose.
 
V

Victor Bazarov

Matthias said:
Too bad not all objects are local :)

You don't need to create all objects local. You just need to create
ONE more local class and an object of it:

void function_that_throws() {
struct call_me_finally_for_all_I_care {
~call_me_finally_for_all_I_care() {
// do your non-local clean-up
}
} janitor;
try { throw 42; }
catch(...) { throw; }
}

V
 
I

Ioannis Vranos

Matthias said:
Java also has a finally construct. IIRC it makes sure that the specific
block is /always/ executed, regardless what happens. Would be nice to
have that in C++ too, for cleanup purposes for example.


In C++ it is not needed since we have RAII, but I guess you will happy
to hear that C++/CLI also provides a finally construct, in addition to RAII.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top