Sign in Join

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

Mailing lists / fpc-pascal / 2015-05

Finally, exceptions and so on.

10 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

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

Mark Morgan Lloyd

Starting off with this fragment

begin // Start of procedure/function
   try
     ...
     if something then
       exit;
     ...
   finally
     ...
   end
end;

My understanding is that exit "magically" transfers control into the finally block before exiting the procedure. However to state the obvious, in this fragment

begin // Start of procedure/function
   try
     ...
     if something then
       exit;
     ...
   except
     ...
   end
end;

control doesn't go via the except block.

Is there a single place in the documentation where this sort of behaviour is described and prioritised, taking into account interfaces etc.?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Graeme Geldenhuys
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
control doesn't go via the except block. Is there a single place in the documentation where this sort of

That is expected behaviour. The exit statement doesn't cause an exception, so why would it go to the except code block? try..except documentation should be covered in the FPC Language Reference documentation, probably in the Exceptions chapter.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

Michael Van Canneyt
to Mark Morgan Lloyd: Starting off with this fragment begin // Start of procedure/function try ... if something…
Mark Morgan Lloyd wrote 29 lines
Starting off with this fragment begin // Start of procedure/function try ... if something then exit; ... finally ... end end; My understanding is that exit "magically" transfers control into the finally block before exiting the procedure. However to state the obvious, in this fragment begin // Start of procedure/function try ... if something then exit; ... except ... end end; control doesn't go via the except block.

That is as designed. There is no exception, so no need to go in the exception.

Is there a single place in the documentation where this sort of behaviour is described and prioritised, taking into account interfaces etc.?

http://www.freepascal.org/docs-html/ref/refse103.html#x214-22400017.3

What does 'taking into account interfaces' have to do with exceptions ? They are unrelated.

Michael.

Reply by email

Ewald
to Mark Morgan Lloyd: Starting off with this fragment begin // Start of procedure/function try ... if something…
Mark Morgan Lloyd wrote
Is there a single place in the documentation where this sort of behaviour is described and prioritised,

Your first fragment is documented on
http://www.freepascal.org/docs-html/ref/refse103.html At the bottom of that page: `Note that an Exit statement enclosed by a try .. finally block, will still execute the finally block.`

The second fragment seems obvious at exit does not raise an exception.

taking into account interfaces etc.?

Why should interfaces have anything to do with exception handing (any more than strings for example)?

Ewald

Reply by email

Mark Morgan Lloyd
to Michael Van Canneyt: That is as designed. There is no exception, so no need to go in the exception.…
Michael Van Canneyt wrote 34 lines
On Thu, 14 May 2015, Mark Morgan Lloyd wrote: > Starting off with this fragment > > begin // Start of procedure/function > try > ... > if something then > exit; > ... > finally > ... > end > end; > > My understanding is that exit "magically" transfers control into the > finally block before exiting the procedure. However to state the > obvious, in this fragment > > begin // Start of procedure/function > try > ... > if something then > exit; > ... > except > ... > end > end; > > control doesn't go via the except block. That is as designed. There is no exception, so no need to go in the exception.

Yes, obviously. However I'm interested in the cases where (1) a try-finally is embedded in a try-except and (2) try-except is embedded in try-finally, and the order that exit and any potential equivalents disentangles things.

Show quoted text 8 lines
> Is there a single place in the documentation where this sort of > behaviour is described and prioritised, taking into account interfaces > etc.? http://www.freepascal.org/docs-html/ref/refse103.html#x214-22400017.3 What does 'taking into account interfaces' have to do with exceptions ? They are unrelated.

Except that- as I understand it- when the reference count is decremented to oblivion, it will be destroyed by "magic" code in the context of the block it was created in. Same obviously applies to strings, dynamic arrays and so on. Are there any other cases where this sort of thing happens?

I've got a horrible feeling that I'm not expressing my question well.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Jonas Maebe
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
Except that- as I understand it- when the reference count is decremented to oblivion, it will be destroyed by "magic" code in the context of the block it was created in.

At which point exactly they will be destroyed is an implementation detail that is expressly undefined.

Jonas

Reply by email

Mark Morgan Lloyd
to Jonas Maebe
Jonas Maebe wrote 7 lines
On 14/05/15 13:18, Mark Morgan Lloyd wrote: > Except that- as I understand it- when the reference count is decremented > to oblivion, it will be destroyed by "magic" code in the context of the > block it was created in. At which point exactly they will be destroyed is an implementation detail that is expressly undefined.

Thanks very much for that Jonas. So I think it leaves the only "magic" being that exit always goes via the live finally blocks, or is there equivalent behaviour for raise?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Mark Morgan Lloyd
to Ewald: Your first fragment is documented on http://www.freepascal.org/docs-html/ref/refse103.html…
Ewald wrote 8 lines
On 05/14/2015 12:08 PM, Mark Morgan Lloyd wrote: > Is there a single place in the documentation where this sort of > behaviour is described and prioritised, Your first fragment is documented on http://www.freepascal.org/docs-html/ref/refse103.html At the bottom of that page: `Note that an Exit statement enclosed by a try .. finally block, will still execute the finally block.`

That's definitely the most useful page I've found, but the pertinent bit is

"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. [...] and then the exception will be automatically re-raised." (minor spelling mistakes in 2.6.4 17.3).

So in effect, both an exit and a raise first execute the innermost finally, then look for procedure end or exception block repeating as necessary. These are the only cases where syntax needs to be unwound in this way.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Ewald
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
So in effect, both an exit and a raise first execute the innermost finally, then look for procedure end or exception block repeating as necessary.

Yes.

These are the only cases where syntax needs to be unwound in this way.

Well... There is also continue and break. The following program will also execute the Finally statements before exiting the loop:

==== Code Begin ====
Program TestFinally;
{$mode ObjFPC}
Procedure Whatever;
Begin
    WriteLn('Finally.');
End;
Begin
    While True do
        Try
            Break
        Finally
            Whatever
        End;
    Repeat
        Try
            Continue
        Finally
            Whatever
        End
    Until True;
End.
==== Code End ====
Ewald

Reply by email

Mark Morgan Lloyd
to Ewald
Ewald wrote 12 lines
On 05/14/2015 04:17 PM, Mark Morgan Lloyd wrote: > So in effect, both an exit and a raise first execute the innermost > finally, then look for procedure end or exception block repeating as > necessary. Yes. > These are the only cases where syntax needs to be unwound in this way. > Well... There is also continue and break. The following program will also execute the Finally statements before exiting the loop:

Thanks for that. Any others that occur to anybody?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email