Author Topic: Need this code to report any error.  (Read 311 times)

user5

  • Sr. Member
  • ****
  • Posts: 428
Need this code to report any error.
« on: October 13, 2026, 02:02:00 am »
    The following code works fine, as well or better than CopyFile, but could someone show me how to write it so that it displays any error with ShowMessage?

 
Code: Pascal  [Select][+][-]
  1. SrcStream := TFileStream.Create('zoom2media\zoomer2.avi', fmOpenRead or fmShareDenyWrite);
  2.   try
  3.     DstStream := TFileStream.Create(filename, fmCreate);
  4.     try
  5.       repeat
  6.         ReadBytes := SrcStream.Read(Buffer, SizeOf(Buffer));
  7.         if ReadBytes > 0 then
  8.         begin
  9.           DstStream.Write(Buffer, ReadBytes);
  10.           Application.ProcessMessages;
  11.         end;
  12.       until ReadBytes = 0;
  13.     finally
  14.       DstStream.Free;
  15.     end;
  16.   finally
  17.     SrcStream.Free;
  18.   end;  

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: Need this code to report any error.
« Reply #1 on: October 13, 2026, 06:59:14 am »
Code: Pascal  [Select][+][-]
  1. SrcStream := TFileStream.Create('zoom2media\zoomer2.avi', fmOpenRead or fmShareDenyWrite);
  2. try
  3.   DstStream := TFileStream.Create(filename, fmCreate);
  4.   try
  5.     try
  6.       repeat
  7.         ReadBytes := SrcStream.Read(Buffer, SizeOf(Buffer));
  8.         if ReadBytes > 0 then
  9.         begin
  10.           DstStream.Write(Buffer, ReadBytes);
  11.           Application.ProcessMessages;
  12.         end;
  13.       until ReadBytes = 0;
  14.     except
  15.       On e:exception do
  16.         // shows the real exception class + the message
  17.         showmessage(e.classname+':'+e.message);
  18.     end;
  19.   finally
  20.     DstStream.Free;
  21.   end;
  22. finally
  23.   SrcStream.Free;
  24. end;  
Note that this is not perfect because Application.Processmessages is an escape route on which execution exceptions can occur from outside this routine. But that is the cost of using Application.ProcessMessages instead of a thread.
It is usually fine.
« Last Edit: October 13, 2026, 07:12:08 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

user5

  • Sr. Member
  • ****
  • Posts: 428
Re: Need this code to report any error.
« Reply #2 on: October 13, 2026, 11:32:53 am »
    Your suggested code ran okay. I wanted to test if it would report an error so for testing purposes I put a division by zero statement into the repeat statement and ran it.
    The program didn't crash or hang up but it did not report the error. I wonder if there is another way to see if it reports an error. Thanks for your response.

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: Need this code to report any error.
« Reply #3 on: October 13, 2026, 11:39:15 am »
What platform? I tested a - made up - complete program on Windows 11 and that works.
But it is better - and more complex - to use a thread and synchronize.

If ShowMessage did not show up on Windows, there are other errors in your code...
I can test on Linux and Apple later. (Linux + Qt6 works OK)
[edit]On some widgetsets the showmessage can be hidden behind the main form!! Move the main form...then click the title bar of the showmessage...On some platforms ShowMessage is not modal and stayontop.
« Last Edit: October 16, 2026, 06:17:08 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018