|
|||
|
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. |
|
|
||||
|
||||
|
|
|
|||
|
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 |
|
|||
|
"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. |
|
|||
|
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 |
|
|||
|
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 .. |
|
|||
|
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.. |
|
|||
|
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. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |