Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

Error handling

Whilst trying to open several image files at once I have come across a situation where a file throws up several exceptions namely EInvalidGraphic EInvalidOperation EAccessViolation. Basically what I am doing is

var
i: integer
begin
for i := 0 to FileListBox1.Count -1 do
begin
  create image stuff
  loadfromfile etc
end;

I am using this for thumbnail/slideshow effect. I've used try except for trapping one error but can't do it for all. Is ther a universal try except that will trap all possible errors or how do I use more than one try except. What I want is to ignore the file that creates the exception and only exit from the loop when all files have been attempted to open. At the moment if I have say 16 files in the directory and the fourth raises exception it either stops at 4 or stops at 4 and hangs.

regards
Chris
0
xpher
Asked:
xpher
  • 17
  • 15
  • +1
1 Solution
 
EpsylonCommented:
Are you aware of the fact that using try..except still raises exceptions when you run the app inside the Delphi environment.

Also take a look at TApplication.OnException
0
 
vinnairCommented:
The problem can be solved using a single try..except block. The try except block however should trap all the possible exceptions which have been mentioned above by you. This can be done using the on keyword.

thus the try...except block will look something like..

try
  //your code here
except
  on EInvalidOperation: //ignore
  on EInvalidGraphic: //ignore
  //other exceptions
end;

It will be better also if a seperate procedure is kept for opening of a file so that the organization of the code is better. This procedure can be called as open file and it can be used for the simple operation of opening a file.

Also as 'epsylon' had mentioned, exceptions are anyway raised in the delphi environment. So run it outside delphi after compilation, of course

Hope the above solves your problem

Best of luck
0
 
xpherAuthor Commented:
Yes I realise about running in Delphi environment, I always setup a shortcut on my quick launch bar to my project.

Tried both your ideas and interestingly enough with the problem image file no exceptions are raised but there is continual disk activity as though it is still trying to open the file but my using ctrl-alt-del shows my application as not responding, and it isn't. I've tried opening the file in another application and this gives me an error that file can't be read.

regards
chris
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
EpsylonCommented:
Show us some code then, otherwise we can't help you I guess...
0
 
xpherAuthor Commented:
OK this is one idea I was trying:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, FileCtrl, ExtCtrls;

type
  TForm1 = class(TForm)
    DriveComboBox1: TDriveComboBox;
    DirectoryListBox1: TDirectoryListBox;
    FileListBox1: TFileListBox;
    ScrollBox1: TScrollBox;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const
  xdist: integer = 10;
  ydist: integer = 10;

var
  MyImage: TImage;



procedure TForm1.BitBtn1Click(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to FileListBox1.Items.Count - 1 do
  try
  begin
    MyImage := TImage.Create(self);
    with MyImage do
    begin
      Parent := ScrollBox1;
      Height := 100;
      Left := xdist;
      Picture.LoadFromFile(FileListBox1.Items.Strings[i]);
      Stretch := True;
      Top := ydist;
      Width := 100;
    end;
    xdist := MyImage.Left + MyImage.Width + 10;
  end;
  except
    on EReadError do ShowMessage('Error reading file');
    on EInvalidOperation do ShowMessage('Error reading file');
    on EAccessViolation do ShowMessage('Error reading file');
    on EInvalidGraphic do ShowMessage('Error reading file');
  end;
end;

end.

regards
Chris
0
 
EpsylonCommented:
Try this:

  except
    on EReadError do ShowMessage('Error reading file');
    on EInvalidOperation do ShowMessage('Error reading file');
    on EAccessViolation do ShowMessage('Error reading file');
    on EInvalidGraphic do ShowMessage('Error reading file');
  else
    HandleAllOthers;
  end;

or just

except
  ShowMessage('......');
end;
0
 
EpsylonCommented:
TApplication.OnException should trap all this too....
0
 
xpherAuthor Commented:
You are right it does.
But I still have the problem as mentioned earlier that if I have say 16 files in the directory and the fourth raises exception it stops at 4 and instead of skipping and carrying on to open the next available file it just stops at 4.

regards
Chris
0
 
xpherAuthor Commented:
Sorry ignore the last comment I hadn't seen the next to las comment you left me. Yes that overcomes the problem of stopping at a certain item. But still have problem with this one file wanting to open and then it throws a runtime error when it comes to displaying the image file. How do you trap for that kind of eventuality?

regards
Chris
0
 
EpsylonCommented:
Perhaps this is more suitable:

  for i := 0 to FileListBox1.Items.Count - 1 do
  begin
    MyImage := TImage.Create(self);
    try
      with MyImage do
      begin
        Height := 100;
        Left := xdist;
        Picture.LoadFromFile(FileListBox1.Items.Strings[i]);
        Parent := ScrollBox1;
        Stretch := True;
        Top := ydist;
        Width := 100;
      end;
      xdist := MyImage.Left + MyImage.Width + 10;
    finally
      MyImage.Free;
    end;
  end;



Regards,

Epsylon.
0
 
xpherAuthor Commented:
Using MyImage.Free means that there is nothing to see.

regards
Chris
0
 
xpherAuthor Commented:
When I put the try before the i := 0 etc. the images show.

When I scroll the scrollbox and as I am nearing the bad image I now get a message of EOutOfResources promptly followed by a message saying 'canvas does not allow drawing'

regards
Chris
0
 
EpsylonCommented:
Oops I typed 'finally' by accident. It should be 'except'.
0
 
EpsylonCommented:
I tested it and it seems to work  :o)
0
 
xpherAuthor Commented:
Mmm yes that does, but I think the problem I have is actually the file itself but I need to be able to trap the problem. Using the Image.Free idea when I am scrolling through the images (in ScrollBox) as soon as I get to the problem image I get a small message box with no title and the message 'Exception A@' also at the same time I get a message box with title Project1 and message 'Canvas does not allow drawing'. By this time there is a multitude of grey buttons on the taskbar and then a message box appears with 'Runtime Error 216 at 00002D 38'.
If I say ok to this all the grey buttons disappear and the application closes down. So whatever is wrong with this file it seems to open but not display or be allowed to, I think it is this that I somehow need to trap/bypass.

You've been a great help so far but any more ideas.

Much appreciated
Chris
0
 
EpsylonCommented:
I think there's some resource problem. How many picture are there and how big are they?
0
 
xpherAuthor Commented:
30 files and the problem one is 18.8kb and others vary from 4kb up to 20kb.

I've used this idea with a directory that had 125 files with no problems.

regards
Chris
0
 
xpherAuthor Commented:
Damn maybe should have said it's a gif file that is causing probs. I just tried GIFOptimizer and it came up with message 'premature end of data', is there any way I can trap and display this with my app?

regards
Chris

ps if you think it requires more points please say
0
 
xpherAuthor Commented:
Damn maybe should have said it's a gif file that is causing probs. I just tried GIFOptimizer and it came up with message 'premature end of data', is there any way I can trap and display this with my app?

regards
Chris

ps if you think it requires more points please say
0
 
EpsylonCommented:
Note: TImage does not support gif!

The code as it is now should trap all exceptions. To test this I did I copied some jpg, gif, bmp and other files into the project dir. Some of the jpg and gif files I damaged on purpose.
All bad and unknown files were trapped successfully by the 'try..except' statement in Delphi 3 and 5.

So my question is: What are you doing that causes a gif file to screw-up the whole thing?
0
 
xpherAuthor Commented:
Nothing out of the ordinary. I'm opening the file just the same as any other. I have tried to open it in various profesional image editors some can open it some can't. But the ones that can't show a message can't read etc. or 'premature end of data'.

Would you like the image file to try yourself?

Cheers
Chris  
0
 
xpherAuthor Commented:
By the way this url should lead you to the offending file.
http://www.free-clipart.net/cgi-bin/imageSearch.cgi?img=0&search=E_ANI.GIF&cat=all

cheers
Chris
0
 
EpsylonCommented:
Tried it. The image does NOT show and does NOT raise any exception. I only get jpg's and bmp's in the scrollbox...
0
 
xpherAuthor Commented:
Weird. Just tried file on another machine and same problem of 'premature end of data', tried my app on machine to load file and same problem of trying to open file with continuous disc activity and app not responding.

cheers
Chris
0
 
EpsylonCommented:
Just to be sure, this is the code I have right now:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, FileCtrl, JPEG;

type
  TForm1 = class(TForm)
    Button1: TButton;
    FileListBox1: TFileListBox;
    ScrollBox1: TScrollBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const
  xdist: integer = 10;
  ydist: integer = 10;

var
  MyImage: TImage;

procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
  for i := 0 to FileListBox1.Items.Count - 1 do
  begin
    MyImage := TImage.Create(self);
    try
      with MyImage do
      begin
        Height := 100;
        Left := xdist;
        Picture.LoadFromFile(FileListBox1.Items.Strings[i]);
        Parent := ScrollBox1;
        Stretch := True;
        Top := ydist;
        Width := 100;
      end;
      xdist := MyImage.Left + MyImage.Width + 10;
    except
      MyImage.Free;
    end;
  end;
end;

end.
0
 
xpherAuthor Commented:
Yes but I also have GIFImage in my uses.
This is Anders Melander? GIFimage which integrates into image component.

Cheers
Chris
0
 
EpsylonCommented:
Now we're getting somewhere.
I think that this component does have it's own error handling and traps it before you can trap it. Don't know if you can turn it off or something.

Where can I find that component?
0
 
xpherAuthor Commented:
The url is
http://www.melander.dk/delphi/gifimage

cheers
Chris

ps I'm using v2.9
I also have tried a couple of other GIF components with similar effect
0
 
EpsylonCommented:
Can't find v2.9 so I tried v2.02 (minimal source) and commented-out line 1554:

//raise GIFException.Create(msg) at ReturnAddr;


Now the E_ANI.gif is displayed correctly but bad gif files (the ones I damaged on purpose) still cause trouble.
0
 
EpsylonCommented:
Correction: I tried v2.2
0
 
xpherAuthor Commented:
Oops yes it was version 2.2 I was looking at something else.

Interestingly enough I have just tried opning the offending file using the RXGifAnimator from the RxLib and this has no problem opening the file. The only problem now is I was hoping not to have to use two different units.

Cheers
Chris
0
 
nricoCommented:
I haven't read everything, but I have a comment on the "Exceptions under Delphi environment" thing -- You can disable those in the Tools/Debugger options/Exceptions menu.

You can set whether or not to break at exceptions or to ignore certain exceptions.
0
 
EpsylonCommented:
We are aware of that (see the 1st comment), only noone mentioned how to disable it...

The problem is the gif component. When it encounters corrupt gif files it crashed very badly. Even the Delphi debugger doesn't trap it.....
0
 
xpherAuthor Commented:
Hi Epsylon
I've decided to use RxLib gif component it has some good features and solves my problem with the offending file(s), (along with your excellent help on error handling of course).
Interestingly enough with the RxGif I can recreate the earlier problem by setting its AsyncDrawing property to true. I presume this is something to do with the way the gif is drawn on the canvas?

Many thanks for all your help

Chris :0)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 17
  • 15
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now