Board index » delphi » Stream Exception Handling

Stream Exception Handling

Howdy,
trying to use a couple of streams for no-nonsense file copying.  Works
great - until - I had a corrupted file on the source and the damned
thing hangs my Delphi app.  Can't find squat on the stream exceptions
- tho I know it has to drill down to a file handle somewhere... anyway
- seems like it needs some sort of recovery but I can't find out what
kind - here's a snippet - can anyone help?  TIA Jon

     Strm1 := TFileStream.Create(srcname,fmOpenRead);
     Strm2 := TFileStream.Create(destname,fmOpenWrite or fmCreate);
     try
       Strm2.CopyFrom(Strm1,Strm1.Size); { Set bytes in motion }
     except
      on EInOutError do
       ErrLabel.Caption := 'File Err ' + destname ;
     end;
     Strm1.Free; { Release them }
     Strm2.Free;

 

Re:Stream Exception Handling


Quote
J. Smith wrote:

> Howdy,
> trying to use a couple of streams for no-nonsense file copying.  Works
> great - until - I had a corrupted file on the source and the damned
> thing hangs my Delphi app.  Can't find squat on the stream exceptions
> - tho I know it has to drill down to a file handle somewhere... anyway
> - seems like it needs some sort of recovery but I can't find out what
> kind - here's a snippet - can anyone help?  TIA Jon

>      Strm1 := TFileStream.Create(srcname,fmOpenRead);
>      Strm2 := TFileStream.Create(destname,fmOpenWrite or fmCreate);
>      try
>        Strm2.CopyFrom(Strm1,Strm1.Size); { Set bytes in motion }
>      except
>       on EInOutError do
>        ErrLabel.Caption := 'File Err ' + destname ;
>      end;
>      Strm1.Free; { Release them }
>      Strm2.Free;

        Aha - this makes more sense than the previous version of the question.
If there's an exception then the "except" part is executed and then the
current block (whatever function we're in) is exited. So in your code the
streams are never freed on exception. Try this:

      Strm1 := TFileStream.Create(srcname,fmOpenRead);
      Strm2 := TFileStream.Create(destname,fmOpenWrite or fmCreate);
      try
        Strm2.CopyFrom(Strm1,Strm1.Size); { Set bytes in motion }
      except
       on EInOutError do
         begin
          ErrLabel.Caption := 'File Err ' + destname ;
          Strm1.Free; { Release them }
          Strm2.Free;
         end;
      end;

        I don't know that it's going to fix your problem, but it's
the right way to do it in any case.

--
David Ullrich

sig.txt not found

Re:Stream Exception Handling


Quote
J. Smith wrote:

> Howdy,
> trying to use a couple of streams for no-nonsense file copying.  Works
> great - until - I had a corrupted file on the source and the damned
> thing hangs my Delphi app.  Can't find squat on the stream exceptions
> - tho I know it has to drill down to a file handle somewhere... anyway
> - seems like it needs some sort of recovery but I can't find out what
> kind - here's a snippet - can anyone help?  TIA Jon

>      Strm1 := TFileStream.Create(srcname,fmOpenRead);
>      Strm2 := TFileStream.Create(destname,fmOpenWrite or fmCreate);
>      try
>        Strm2.CopyFrom(Strm1,Strm1.Size); { Set bytes in motion }
>      except
>       on EInOutError do
>        ErrLabel.Caption := 'File Err ' + destname ;
>      end;
>      Strm1.Free; { Release them }
>      Strm2.Free;

  I don't why your application hanged, Delphi should generally raise
exceptions if there is a file problem (since the VCL is compiled with
file i/o exceptions enabled, I assume).
  By the way, you should also trap EStreamError, and the copy process
could even raise an EOutOfMemory too (whether you trap that or not is up
to you).
  On another note... I forget exactly, but is fmOpenWrite a good idea ?
fmCreate should delete the file if it exists anyway, and you don't want
to write in to an existing file that might be larger than the source
file your copying from, leaving some extra data in the destination file
?.

Andrew

Re:Stream Exception Handling


Quote
>  I don't why your application hanged, Delphi should generally raise
>exceptions if there is a file problem (since the VCL is compiled with
>file i/o exceptions enabled, I assume).

It did.  The message pops up and then it just sits there thinking
apparently it is still moving stuff around.  I just considered
possibly I need to re-raise the exception.  Whaddya think?

Quote
>  By the way, you should also trap EStreamError,

Hmm that may be something to try...

I guess what I'm after here is - where do I go to look for how to deal
with stream errors - you know the parable about teaching a man to fish
versus feeding him for one day?

Other Threads