Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-06

try finally exit

23 messages · 13 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Junior
strList.TStringList.Create;
try
   if Pos('blablabla',strList.Text) = 0 then
     Exit;
finally
    strList.Free;
end;

Adding "Exit", the finally is executed?

thanks

Reply by email

Junior
to Bart: Just wondering. Why couldn't you test that yourself? Bart

please be patient.

Bart wrote 23 lines
On 6/28/13, Junior wrote: > strList.TStringList.Create; > try > if Pos('blablabla',strList.Text) = 0 then > Exit; > > finally > strList.Free; > end; > > Adding "Exit", the finally is executed? > Just wondering. Why couldn't you test that yourself? Bart -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

John Meyer
to Junior: 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?

Junior wrote 34 lines
because I use a production machine plese be patiente! Em 28-06-2013 17:34, Bart escreveu: On 6/28/13, Junior wrote: >> strList.TStringList.Create; >> try >> if Pos('blablabla',strList.Text) = 0 then >> Exit; >> >> finally >> strList.Free; >> end; >> >> Adding "Exit", the finally is executed? >> > Just wondering. Why couldn't you test that yourself? > > Bart > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Junior
to John Meyer: 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?

John Meyer wrote 45 lines
pray tell what does using a production machine have to do with testing a trivial section of code? Junior wrote: > because I use a production machine > > plese be patiente! > > Em 28-06-2013 17:34, Bart escreveu: > On 6/28/13, Junior wrote: >>> strList.TStringList.Create; >>> try >>> if Pos('blablabla',strList.Text) = 0 then >>> Exit; >>> >>> finally >>> strList.Free; >>> end; >>> >>> Adding "Exit", the finally is executed? >>> >> Just wondering. Why couldn't you test that yourself? >> >> Bart >> >> -- >> _______________________________________________ >> Lazarus mailing list >> >> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus >> > > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Junior
to Antônio: try if true then exit; finally showmessage(''); end; Ant?nio -------------- next part…

good idea, thank

Ant?nio wrote 14 lines
try if true then exit; finally showmessage(''); end; Ant?nio -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130629/4990ee64/attachment.html>

Reply by email

Jürgen Hestermann
to Bart: Just wondering. Why couldn't you test that yourself? Bart
Bart wrote 10 lines
On 6/28/13, Junior wrote: > strList.TStringList.Create; > try > if Pos('blablabla',strList.Text) = 0 then > Exit; > finally > strList.Free; > end; > Adding "Exit", the finally is executed? Just wondering. Why couldn't you test that yourself?

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?

Reply by email

Hans-Peter Diettrich
to Junior: this code was just one example, I have of putting all the code for ask this?
Show quoted text 8 lines
this code was just one example, I have of putting all the code for ask this? Em 28-06-2013 20:52, John Meyer escreveu: > pray tell what does using a production machine have to do with testing > a trivial section of code? > Junior wrote: >> because I use a production machine

[...]

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

Reply by email

Hans-Peter Diettrich
to Jürgen Hestermann: Is that the way programming is done today: "There is no documentation, just figure it out…
Show quoted text 15 lines
Am 2013-06-28 22:34, schrieb Bart: > On 6/28/13, Junior wrote: >> strList.TStringList.Create; >> try >> if Pos('blablabla',strList.Text) = 0 then >> Exit; >> finally >> strList.Free; >> end; >> Adding "Exit", the finally is executed? > Just wondering. Why couldn't you test that yourself? Is that the way programming is done today: "There is no documentation, just figure it out yourself"? I can't believe it.

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

Reply by email

Mattias Gaertner
to Hans-Peter Diettrich
Show quoted text 19 lines
J?rgen Hestermann schrieb: > Am 2013-06-28 22:34, schrieb Bart: > > On 6/28/13, Junior wrote: > >> strList.TStringList.Create; > >> try > >> if Pos('blablabla',strList.Text) = 0 then > >> Exit; > >> finally > >> strList.Free; > >> end; > >> Adding "Exit", the finally is executed? > > Just wondering. Why couldn't you test that yourself? > > Is that the way programming is done today: "There is no documentation, > just figure it out yourself"? > I can't believe it. Such documentation exists in most cases, but it may be hard to find, in detail in a Wiki :-(

The language syntax is not described in the wiki. It is available as pdf and html.

Mattias

Reply by email

Jürgen Hestermann
to Mattias Gaertner
Mattias Gaertner wrote
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 ? 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.

Reply by email

Ludo Brands
to Jürgen Hestermann: Is that the way programming is done today: "There is no documentation, just figure it out…
J?rgen Hestermann wrote 10 lines
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?

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

Reply by email

Mattias Gaertner
to Jürgen Hestermann: 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.

Show quoted text 9 lines
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.

Michael Van Canneyt maintains these docs.
I will ask him.

Mattias

Reply by email

Jürgen Hestermann
to Ludo Brands: Google for "freepascal try finally", first hit. Or in lazarus hit F1. You get all the…
Ludo Brands wrote
In 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.

Reply by email

Hans-Peter Diettrich
to Ludo Brands: Google for "freepascal try finally", first hit. Or in lazarus hit F1. You get all the…
Show quoted text 12 lines
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;

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

Reply by email

Benito van der Zander
to Mattias Gaertner: 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

Mattias Gaertner wrote 29 lines
On Sat, 29 Jun 2013 14:46:26 +0200 J?rgen Hestermann wrote: > 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. > 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. Michael Van Canneyt maintains these docs. I will ask him. Mattias -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Eric Kom
to Michael Van Canneyt
Michael Van Canneyt wrote 11 lines
On Sat, 29 Jun 2013, Benito van der Zander wrote: > It 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.

Thanks

Show quoted text 8 lines
Michael. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Kind Regards Eric Kom System Administrator & Programmer - Metropolitan College _________________________________________ / You are scrupulously honest, frank, and \ | straightforward. Therefore you have few | \ friends. / ----------------------------------------- \ \ .--. |o_o | |:_/ | // \ \ (| Kom | ) /'\_ _/`\ \___)=(___/ 2 Hennie Van Till, White River, 1240 Tel: 013 750 2255 | Fax: 013 750 0105 | Cell: 078 879 1334 | www.kom.za.net | www.kom.za.org | www.erickom.co.za Key fingerprint: 513E E91A C243 3020 8735 09BB 2DBC 5AD7 A9DA 1EF5

Reply by email