Recent

Author Topic: How do you use try... finally  (Read 9816 times)

cybersmyth

  • Jr. Member
  • **
  • Posts: 51
How do you use try... finally
« on: March 30, 2011, 03:50:11 pm »
The application I'm working on right now creates a file using TFileStream. Every now and then I get an error saying it can't create the file. It's working fine 90% of the time, and I'm having a hard time tracking down why it's not working the other 10%. But in any event, I believe the "try... finally" command is suppose to be a way of catching these types of things? I can try to open the stream and if it doesn't work just report it to the user without crashing the program? Am I right on that? If so could someone help me out with how use try... finally? And if I'm off base totally can someone point me in the right general direction?

Thanks!

DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: How do you use try... finally
« Reply #1 on: March 30, 2011, 04:13:33 pm »
try...finally is normally used to do some cleanup in case of an exception (see e.g. http://www.freepascal.org/docs-html/ref/refse79.html); it does not handle the exception itself.

If you want to show an error message to the user (or log it somehow) you should use try...except (http://www.freepascal.org/docs-html/ref/refse78.html).

It is also possible to nest these two so you can clean up *and* handle the exception.
Code: Pascal  [Select][+][-]
  1. try
  2.   try
  3.   ...
  4.   finally
  5.     ...
  6.   end;
  7. except
  8.   ...
  9. end;

Gr.
Dirk.


cybersmyth

  • Jr. Member
  • **
  • Posts: 51
Re: How do you use try... finally
« Reply #2 on: March 31, 2011, 12:19:25 am »
What about {$I+} would this work with a TFileStream?

Code: Pascal  [Select][+][-]
  1.       {$I-}
  2.       File :=TFileStream.Create('C:\afile.txt', fmCreate or fmOpenWrite);;
  3.       {$I+}
  4.       IOR := IOResult;
  5.       if IOR <> 0 then
  6.         begin
  7.           ReportAnError;
  8.           FreeAndNil(File);
  9.         end
  10.       else
  11.         begin
  12.           DoSomethingToFile;
  13.           FreeAndNil(File);
  14.        end;
  15.  
  16.  
  17.  
  18.  
  19.  

Leledumbo

  • Hero Member
  • *****
  • Posts: 8799
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How do you use try... finally
« Reply #3 on: March 31, 2011, 05:53:11 am »
Quote
What about {$I+} would this work with a TFileStream?
Just try yourself, though I don't think it would. If the code was designed to raise an exception when an error occurs, then it will still raise the exception regardless of {$I} directive settings.

cybersmyth

  • Jr. Member
  • **
  • Posts: 51
Re: How do you use try... finally
« Reply #4 on: March 31, 2011, 07:59:38 pm »
Thanks for the advice. I found this tutorial, and it looks like it takes care of the job.

http://www.delphibasics.co.uk/Article.asp?Name=Exceptions

mp456

  • Newbie
  • Posts: 4
Re: How do you use try... finally
« Reply #5 on: August 06, 2011, 04:05:00 pm »
I try to run this simple example program, to understand how functions "try..exept..finnaly" commands, but my Lazarus v 0.9.30 says:
El proyecto project1 ha lanzado una excepción de la clase 'External: SIGFPE'

the program is:

uses sysutils;
var
  number, zero : Integer;
begin
  // Try to divide an integer by zero - to raise an exception
  Try
    zero   := 0;
    number := 1 div zero;
    WriteLn('number / zero = '+IntToStr(number));
  except
    on E : Exception do
      WriteLn(E.ClassName+' error raised, with message : '+E.Message);
    On E : EDivByZero do
       WriteLn('Div by zero error : '+E.Message);
  end;   readln;
end.   

So, Lazarus terminate the program and there is NO WAY to re-direct error to "except" block

thank you for answer

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: How do you use try... finally
« Reply #6 on: August 06, 2011, 04:22:37 pm »
IIRC here was thread about it. The behaviour was different on GTK2. See: http://www.lazarus.freepascal.org/index.php/topic,13460.0.html
You can also start your executable directly (out of IDE).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

mp456

  • Newbie
  • Posts: 4
Re: How do you use try... finally
« Reply #7 on: August 06, 2011, 04:46:33 pm »
thank you Blazen
You right, outside of IDE -the executable- works perfectly
quick response!
you are a hero :P

 

TinyPortal © 2005-2018