strList.TStringList.Create;
try
if Pos('blablabla',strList.Text) = 0 then
Exit;
finally
strList.Free;
end;Adding "Exit", the finally is executed?
thanks

Mailing lists / lazarus / 2013-06
23 messages · 13 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
strList.TStringList.Create;
try
if Pos('blablabla',strList.Text) = 0 then
Exit;
finally
strList.Free;
end;Adding "Exit", the finally is executed?
thanks
Junior wroteAdding "Exit", the finally is executed?
Yes
strList.TStringList.Create; try if Pos('blablabla',strList.Text) = 0 then Exit; finally…
Yes. your example lacks some example code after Exit, before Finally, that will be omitted.
thanks
-L;strList.TStringList.Create; try if Pos('blablabla',strList.Text) = 0 then Exit; finally…
Just wondering. Why couldn't you test that yourself?
Bart
because I use a production machine
plese be patiente!
Just wondering. Why couldn't you test that yourself? Bart
please be patient.
because I use a production machine plese be patiente!
pray tell what does using a production machine have to do with testing a trivial section of code?
please be patient.
try
if true then exit;
finally
showmessage('');
end;Ant?nio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130628/bc97eed7/attachment.html>
pray tell what does using a production machine have to do with testing a trivial section…
this code was just one example, I have of putting all the code for ask this?
try if true then exit; finally showmessage(''); end; Ant?nio -------------- next part…
good idea, thank
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130629/4990ee64/attachment.html>
Just wondering. Why couldn't you test that yourself? Bart
Is that the way programming is done today: "There is no documentation, just figure it out yourself"? I can't believe it.
How can the programmer be sure that test results apply under all circumstances? Does he have to write test routines for all possible szenarious? What about nested try..finaly commands?
Is it guaranteed that all finally parts are executed?
this code was just one example, I have of putting all the code for ask this?
[...]
For your simple case the provided code is sufficient.
What does "production machine" mean to you? Are you not allowed to write and run your own programs on that machine? If so, you can ask your boss (daddy, administrator...) to create an restricted sandbox account for you, where you can play around with Lazarus without a risk of damage to the system.
If you are allowed to start a program from the Lazarus IDE, you can test its behaviour without modifications to the code. Put an breakpoint somewhere before the "exit" in the try block, and start the program. When that breakpoint is reached, you can single-step through the code (F7) to see which statements are executed in sequence. Without an try-finally block the next executed statement is the subroutine "end;", but from "exit" in the try block, execution proceeds at the code in the "finally" block.
Structured exception handling (SEH) and the use of try-except and try-finally blocks is not easy to understand. Feel free to ask more questions :-)
BTW, look how other people reply to a message. Snip from your reply whatever must not be read again, and insert your comments or questions *after* each related part of the message.
DoDi
Is that the way programming is done today: "There is no documentation, just figure it out…
Such documentation exists in most cases, but it may be hard to find, in detail in a Wiki :-(
How can the programmer be sure that test results apply under all circumstances?
Depends on the test code and results ;-)
Does he have to write test routines for all possible szenarious?
At least for all scenarios where the user doesn't trust the documentation or answers to his questions. Sometimes very special code can reveal compiler bugs, when the program does not behave as it should.
What about nested try..finaly commands?
Think of every try-finally block as a room, with one entry and one exit door. Whenever the try block is left, execution must pass through its exit door (finally block). Only after exiting from all rooms, floors etc. this way, the subroutine is finally left (at its "end;").
Not so obvious: code *after* the finally-end is *not* executed after an "exit;"!
procedure TestExit;
begin
try
WriteLn('enter try-finally block');
exit;
finally
WriteLn('reached finally');
end;
WriteLn('compiler error: this should never be reached!');
end;Is it guaranteed that all finally parts are executed?
Yes, even in less obvious cases. When an exception occurs, *all* pending finally blocks execute, in *all* subroutines on the call stack. "Pending" here means those try-finally blocks, which had been entered before the subroutine call, but have not alreday been left. This *stack unwinding* proceeds until the top level subroutine, or until the exception is caught by an try-except block. The exception handler (in this except block) resets the exception, so that from this point on execution proceeds as usual.
The major difference between try-finally and try-except is that "finally" code executes *always*, regardless of how the try-finally block is left. The "except" code instead is executed *only* after an exception occured, not during normal execution. When an exception handler does *not* handle the current exception type, or re-raises the exception, or raises another exception, stack unwinding continues until the next enclosing try-except block.
DoDi
The language syntax is not described in the wiki. It is available as pdf and html.
Mattias
Mattias Gaertner wroteThe language syntax is not described in the wiki. It is available as pdf and html.
You mean this: http://www.freepascal.org/docs-html/rtl/system/exit.html ? How does this answer the question of this thread?
It says:
Exit exits the current subroutine, and returns control to the calling routine. If invoked in the main program routine, exit stops the program.
Where does it tell that any finaly block will still be executed? Just the opposite: It suggests that exit always exits immediately without executing any other code of the routine.
This seems to be not true.
Is that the way programming is done today: "There is no documentation, just figure it out…
Google for "freepascal try finally", first hit. Or in lazarus hit F1. You get all the answers you need.
In case that is all too difficult and moaning on the ML is the preferred way of getting information, here is the contents:
17.3 The try...finally statement
If no exception occurs inside the statement List, then the program runs as if the Try, Finally and End keywords were not present, unless an exit command is given: an exit command first executes all statements in the finally blocks before actually exiting.
If, however, an exception occurs, the program flow is immediatly transferred from the point where the excepion was raised to the first statement of the Finally statements.
All statements after the finally keyword will be executed, and then the exception will be automatically re-raised. Any statements between the place where the exception was raised and the first statement of the Finally Statements are skipped.
As an example consider the following routine:
Procedure Doit (Name : string);
Var F : Text;
begin
Try
Assign (F,Name);
Rewrite (name);
... File handling ...
Finally
Close(F);
end;
If during the execution of the file handling an execption occurs, then
program flow will continue at the close(F) statement, skipping any file
operations that might follow between the place where the exception was
raised, and the Close statement. If no exception occurred, all file
operations will be executed, and the file will be closed at the end.http://www.freepascal.org/docs-html/ref/refse99.html
Ludo
You mean this: http://www.freepascal.org/docs-html/rtl/system/exit.html ? How does this…
Am 2013-06-29 14:27, schrieb Mattias Gaertner: > The language syntax is not described in the wiki. It is available as pdf and html. You mean this: http://www.freepascal.org/docs-html/rtl/system/exit.html ?
Yes, that would be a good place to add a note about try..finally.
Michael Van Canneyt maintains these docs.
I will ask him.
Mattias
On Sat, 29 Jun 2013 14:12:04 +0200 The language syntax is not described in the wiki. It is
The language syntax is not described in the wiki. It is available as pdf and html.
Right, but the syntax description does not (always) answer questions about *semantics*.
DoDi
Google for "freepascal try finally", first hit. Or in lazarus hit F1. You get all the…
Ludo Brands wroteIn case that is all too difficult and moaning on the ML is the preferred way of getting information, here is the contents:
Be aware that not *I* asked the question about exit and try..finaly. I am just complaining about the useless answers saying that the user should test and find out himself.
an exit command first executes all statements in the finally blocks before actually exiting.
Yes, that would be an appropiate answer to the question. Why not give it in the first place?
If people don't want (or do not have the time) to help, okay. But bashing the asker with "try yourself" is waste of time for everybody.
Google for "freepascal try finally", first hit. Or in lazarus hit F1. You get all the…
That's a questionable example. Which of the actions are required for proper operation of Close?
It's much clearer with objects:
MyObj := TMyClass.Create;
//only now the local variable has an valid value
try
//do something with MyObj
finally
MyObj.Free;
end;Wrong placement of the "try" can result in references to uninitialized local variables in the "finally" code, with unpredicatable effects.
That's why I prefer to initialize local object variables to Nil, before entering an try-finally block where they are created. This pattern allows to use an single try-finally block to protect multiple local objects:
obj1 := nil;
obj2 := nil;
...
try
obj1 := T1.Create;
...
finally
obj1.Free;
obj2.Free;
...
end;DoDi
On Sat, 29 Jun 2013 14:46:26 +0200 Yes, that would be a good place to add a note about…
It supposed to be there already:
http://bugs.freepascal.org/view.php?id=21711
Benito van der Zander wroteIt supposed to be there already: http://bugs.freepascal.org/view.php?id=21711
I extended that with an explicit example,
and added a description in the reference documentation for Exit.
Michael.
Thanks