Recent

Author Topic: Error handling and Pictures  (Read 790 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Error handling and Pictures
« on: October 15, 2020, 02:32:12 pm »
Image := TPicture.Create;
         try
            DetHilfe.Image1.Picture := Image.LoadFromFile('C:\WGKBlck\Images\' +SQLQuery.FieldByName('PicName').AsString);
            DetHilfe.Label1.Caption:= SQLQuery.FieldByName(FeldD).AsString;
            //On Error go To 0
            DetHilfe.ShowModal;
            DetHilfe.Image1.Picture := DetHilfe.Image2.Picture;
            KvonZub := true;
         finally
            Image.Free;
         end;


Guys correct me pls. i Dont know How to handle errors and To Import an Image to Deploy on a Form
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Error handling and Pictures
« Reply #1 on: October 15, 2020, 03:37:41 pm »
It quite depends on what error you're expecting or wanting to trap and from where you're expecting it to come.

Nevertheless, maybe a better way to do what your code is trying would be something like:

Code: Pascal  [Select][+][-]
  1. var
  2.   AFilename: String;
  3. begin
  4.   AFilename := 'C:\WGKBlck\Images\' + SQLQuery.FieldByName('PicName').AsString;
  5.   if FileExists(aFilename) then begin
  6.     DetHilfe.Image1.Picture.LoadFromFile(AFilename);
  7.     DetHilfe.Label1.Caption:= SQLQuery.FieldByName(FeldD).AsString;
  8.     //On Error go To 0 ... what error do you expect?
  9.     DetHilfe.ShowModal;
  10.     DetHilfe.Image1.Picture := DetHilfe.Image2.Picture;
  11.     KvonZub := true;
  12.   end;
  13. end;

Now, for example, if you're trying to guard against the image file being corrupt or not being a handled format, etc. that would be something like:

Code: Pascal  [Select][+][-]
  1. var
  2.   AFilename: String;
  3. begin
  4.   AFilename := 'C:\WGKBlck\Images\' + SQLQuery.FieldByName('PicName').AsString;
  5.   if FileExists(aFilename) then
  6.   try
  7.     DetHilfe.Image1.Picture.LoadFromFile(AFilename);
  8.     DetHilfe.Label1.Caption:= SQLQuery.FieldByName(FeldD).AsString;
  9.     //On Error go To 0 ... what error do you expect?
  10.     DetHilfe.ShowModal;
  11.     DetHilfe.Image1.Picture := DetHilfe.Image2.Picture;
  12.     KvonZub := true;
  13.   except
  14.     on e: EGraphicException do
  15.       ShowMessage('Failed to load image:'
  16.                   + LineEnding
  17.                   + e.Message);
  18.   end;
  19. end;

If you still have doubts you should first read "chapter 17 - Exceptions" of the Reference Guide to get an overview of how errors (exceptions) are handled.

HTH!
« Last Edit: October 15, 2020, 03:40:29 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Error handling and Pictures
« Reply #2 on: October 23, 2020, 10:53:45 am »
Thanks a Lot lucamar :)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

 

TinyPortal © 2005-2018