Recent

Author Topic: what is the point of finally in try finally end;  (Read 6039 times)

dieselnutjob

  • Full Member
  • ***
  • Posts: 137
what is the point of finally in try finally end;
« on: April 01, 2014, 06:43:30 pm »
Comparing these to codes

Code: [Select]
try
  trythisfunction;
finally
  alwaysdothisfunction;
end;

Code: [Select]
try
  trythisfunction;
finally
  ;
end;
alwaysdothisfunction;

Don't they do exactly the same thing?

So what is the point of having a "finally" at all?

thanks, Philip

Bart

  • Hero Member
  • *****
  • Posts: 3452
    • Bart en Mariska's Webstek
Re: what is the point of finally in try finally end;
« Reply #1 on: April 01, 2014, 07:42:30 pm »
Why don't you just try it.
See what happens if an exception occurs in TryThisFunction, and what happens if it does not.

You learn more from experimenting than from me just telling you.

Bart

zeljko

  • Hero Member
  • *****
  • Posts: 1050
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: what is the point of finally in try finally end;
« Reply #2 on: April 01, 2014, 08:37:45 pm »
Comparing these to codes

Code: [Select]
try
  trythisfunction;
finally
  alwaysdothisfunction;
end;

Code: [Select]
try
  trythisfunction;
finally
  ;
end;
alwaysdothisfunction;

Don't they do exactly the same thing?

If your example works ok then it will look same, but it isn't same.
try this:

procedure TestTryFinally;
begin
   try
      exit;
   finally
      writeln('I''m in finally block !');
   end;
   writeln('I will not come here.');
end;

That means: In any case, code in finally block is excuted. In your example if yours trythisfunction; crashes or exits
alwaysdothisfunction() will never execute since it is out of finally block.



dieselnutjob

  • Full Member
  • ***
  • Posts: 137
Re: what is the point of finally in try finally end;
« Reply #3 on: April 01, 2014, 11:39:01 pm »
ok I think I get it.

my understanding:-

if anything in the "try" section causes an exception or crashes then the code in "finally" is guaranteed to still take place, but after that the program will still be crashed or excepted as the guarantee is finished.

This explains why exception handling code is in the "finally" section because it is guaranteed to run, thereby clearing down the exception so that the rest of the code can then still work.

Is that right?

Looking at this example
Code: [Select]
// Previous code
  MySection.Acquire;
  Try
    // Protected code
  Finally
    MySection.Release;
  end;
  // Other code.

from http://www.freepascal.org/docs-html/fcl/syncobjs/tcriticalsection.html

why do they suggest the "Try" and "Protected code"?
is it because if that Protected code crashed then the MySection would be locked forever and other threads would never be able to get in?

does that mean that if the "Protected code" is actually really well written and therefore will never crash that the whole Try Finally End thing isn't really necessary?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: what is the point of finally in try finally end;
« Reply #4 on: April 02, 2014, 04:28:42 pm »
Yes and yes ;)

... I think the point here is that the protected code can also call other code that may not be reliable or code that depends on something (e.g. a db/network connection that may be unreliable)
« Last Edit: April 02, 2014, 04:30:22 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Leledumbo

  • Hero Member
  • *****
  • Posts: 8075
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: what is the point of finally in try finally end;
« Reply #5 on: April 02, 2014, 05:21:18 pm »
Quote
This explains why exception handling code is in the "finally" section because it is guaranteed to run, thereby clearing down the exception so that the rest of the code can then still work.

Is that right?
No, finally is for cleanup. Except is for exception handling. One purpose of finally instead of except is for exception propagation, while still cleaning up locally created resources.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: what is the point of finally in try finally end;
« Reply #6 on: April 02, 2014, 05:26:10 pm »
Oops, Read over "cleaning up the exception". Leledumbo is right there...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

karaba

  • New member
  • *
  • Posts: 49
Re: what is the point of finally in try finally end;
« Reply #7 on: April 02, 2014, 05:47:46 pm »
thing of a concept like
Code: [Select]
// initialization code here object creation and so on.
try
 // what ever it is supposed to be done goes here.
except
  on E: exception do begin
    //staff that needs clean up here usually the stuff initialized above.
    raise E; // ok everything is clean go ahead and raise the exception.
  end;
end;
and reduce it to
Code: [Select]
// initialization code here (object creation and so on).
try
  // what ever it is supposed to be done goes here.
finally
  //staff that needs clean up here usually the stuff initialized above.
end;