Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.pascal.misc > Newsgroup comp.lang.pascal.delphi.misc

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 01-05-2005, 04:13 PM
Eddy Fontaine
Guest
 
Posts: n/a
Default Try...Finally question

Hi,
I am trying to avoid exception during file I/O by
using a try finally statement.

To avoid 'memory leak', i want to be sure that the file
will be closed, if it has been open by the program.

Should i write:

..
..
AssignFile(F,"MyFile.txt);
Reset(F);
Try
WriteLn(F,"This is my string");
Finally
CloseFile(F);
End;
..
Or should i write :

..
Try
AssignFile(F,"MyFile.txt);
Reset(F);
WriteLn(F,"This is my string");
Finally
CloseFile(F);
End;
..
..
In fact, wich is the statement that could cause an exception ?
Thanks for your help !

Eddy.


Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 01-05-2005, 04:36 PM
Rob Kennedy
Guest
 
Posts: n/a
Default Re: Try...Finally question

Eddy Fontaine wrote:
> I am trying to avoid exception during file I/O by
> using a try finally statement.


Try-finally does not help you avoid exceptions. To avoid exceptions
during file I/O, disable the "I/O checking" compiler option. If you do
that, then you're responsible for calling IOResult when you're
interested in knowing whether an I/O procedure succeeded.

> Try
> AssignFile(F,"MyFile.txt);
> Reset(F);
> WriteLn(F,"This is my string");
> Finally
> CloseFile(F);
> End;


I don't know which of those procedures raise exceptions, but I do know
that you shouldn't try to close a file that you haven't opened yet. The
file is only open _after_ Reset returns, so you shouldn't put the call
to Reset within that try-finally block.

--
Rob
Reply With Quote
  #3 (permalink)  
Old 01-05-2005, 06:21 PM
Bruce Roberts
Guest
 
Posts: n/a
Default Re: Try...Finally question


"Eddy Fontaine" <eddy.remove.fontaine@pandora.remove.be> wrote in message
news:41dc2024$0$2572$ba620e4c@news.skynet.be...
> Hi,
> I am trying to avoid exception during file I/O by
> using a try finally statement.
>
> To avoid 'memory leak', i want to be sure that the file
> will be closed, if it has been open by the program.
>
> Should i write:
>
> .
> .
> AssignFile(F,"MyFile.txt);


{$I-}
> Reset(F);

if IOResult = 0
then begin
> Try
> WriteLn(F,"This is my string");

if IOResult <> 0
then // handle write error
> Finally
> CloseFile(F);

if IOResult <> 0
then // handle close error
> End;

end
else // handle the error
{$I+}

> .
> Or should i write :


Definitely not. The form above is the correct layout, it just isn't what you
need to trap the error. An alternate construction would be to wrap the Reset
in a try except statement.


Reply With Quote
  #4 (permalink)  
Old 01-06-2005, 08:13 AM
Eddy Fontaine
Guest
 
Posts: n/a
Default Re: Try...Finally question

Rob and Bruce,

Thanks for your inputs!

I think i understand how it works now ;-) !

Best regards,
Eddy.


Reply With Quote
  #5 (permalink)  
Old 01-06-2005, 10:29 PM
VBDis
Guest
 
Posts: n/a
Default Re: Try...Finally question

Im Artikel <41dc2024$0$2572$ba620e4c@news.skynet.be>, "Eddy Fontaine"
<eddy.remove.fontaine@pandora.remove.be> schreibt:

>AssignFile(F,"MyFile.txt);
>Reset(F);
>Try
> WriteLn(F,"This is my string");
>Finally
> CloseFile(F);
>End;


This arrangement should be correct. Whenever an exception occurs during opening
the file, no CloseFile is required.

DoDi
Reply With Quote
  #6 (permalink)  
Old 01-08-2005, 06:02 PM
Jamie
Guest
 
Posts: n/a
Default Re: Try...Finally question

Eddy Fontaine wrote:

> Hi,
> I am trying to avoid exception during file I/O by
> using a try finally statement.
>
> To avoid 'memory leak', i want to be sure that the file
> will be closed, if it has been open by the program.
>
> Should i write:
>
> .
> .
> AssignFile(F,"MyFile.txt);
> Reset(F);
> Try
> WriteLn(F,"This is my string");
> Finally
> CloseFile(F);
> End;
> .
> Or should i write :
>
> .
> Try
> AssignFile(F,"MyFile.txt);
> Reset(F);
> WriteLn(F,"This is my string");
> Finally
> CloseFile(F);
> End;
> .
> .
> In fact, wich is the statement that could cause an exception ?
> Thanks for your help !
>
> Eddy.
>
>

{$I-}
inoutRes := 0; // clear any ignored error from pior oper.
Assignfile(F,'MyFile.Txt');
Reset(F); // one should use Appeand of Rewrite for txt files.
Case ioResult of
0: While IoResult = do
Begin
writeLn(F,'adasdsadsasad');
// assuming that you may want to add more in
// a loop etc.
End;
101:// Disk Write error, Full ?, Read Only Disk?
102://
103://File Not Open, File does not exist? use Rewrite/append.
else MessageDLg('Some UNknow Error', mterror,[mbok],0);
End;
CloseFile(F);
inoutRes := 0;
-----

that is just a brief ..

Reply With Quote
  #7 (permalink)  
Old 01-08-2005, 06:12 PM
Jamie
Guest
 
Posts: n/a
Default Re: Try...Finally question

VBDis wrote:

> Im Artikel <41dc2024$0$2572$ba620e4c@news.skynet.be>, "Eddy Fontaine"
> <eddy.remove.fontaine@pandora.remove.be> schreibt:
>
>
>>AssignFile(F,"MyFile.txt);
>>Reset(F);
>>Try
>> WriteLn(F,"This is my string");
>>Finally
>> CloseFile(F);
>>End;

>
>
> This arrangement should be correct. Whenever an exception occurs during opening
> the file, no CloseFile is required.
>
> DoDi

notice that that a Writeln is being used for a Text file.
RESET assumes its going to be a read file. direction is
incorrect.
should be using Rewrite/Append
generally its not a good idea to start writing string
data at the start of a file when data there already exists.
i guess you could get away with this using carefully planned
Blocked io..

Reply With Quote
  #8 (permalink)  
Old 01-17-2005, 11:47 PM
Thaddy de Koning
Guest
 
Posts: n/a
Default Re: Try...Finally question

This may seem funny, but under circumstances *any* of these statements
could cause an exception. The one(s) you would want to catch are the
most common ones:

- Lack of diskspace could cause the file creation to fail
- Lack of memory could fail almost evrything else.
- Lack of access rights could cause a write to the file to fail, even if
it opens!

To my mind your first example looks proper, provided there is no need
for feedback to the user, in which case I would use try except instead
of try finally.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: question about data values Frank Schiffel Newsgroup comp.soft-sys.sas 0 06-28-2005 12:01 AM
Re: question about data values toby dunn Newsgroup comp.soft-sys.sas 0 06-27-2005 11:40 PM



All times are GMT. The time now is 09:10 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.