Solved

Error handling

Posted on 2000-03-17
34
371 Views
Last Modified: 2010-08-05
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
Comment
Question by:xpher
  • 17
  • 15
  • +1
34 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2630220
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
 

Expert Comment

by:vinnair
ID: 2630751
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
 
LVL 1

Author Comment

by:xpher
ID: 2631080
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2631118
Show us some code then, otherwise we can't help you I guess...
0
 
LVL 1

Author Comment

by:xpher
ID: 2631276
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2631360
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2631364
TApplication.OnException should trap all this too....
0
 
LVL 1

Author Comment

by:xpher
ID: 2631388
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
 
LVL 1

Author Comment

by:xpher
ID: 2631422
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2631468
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
 
LVL 1

Author Comment

by:xpher
ID: 2631503
Using MyImage.Free means that there is nothing to see.

regards
Chris
0
 
LVL 1

Author Comment

by:xpher
ID: 2631516
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2631660
Oops I typed 'finally' by accident. It should be 'except'.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2631704
I tested it and it seems to work  :o)
0
 
LVL 1

Author Comment

by:xpher
ID: 2631912
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2632166
I think there's some resource problem. How many picture are there and how big are they?
0
 
LVL 1

Author Comment

by:xpher
ID: 2632721
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:xpher
ID: 2633143
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
 
LVL 1

Author Comment

by:xpher
ID: 2633146
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2633269
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
 
LVL 1

Author Comment

by:xpher
ID: 2633311
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
 
LVL 1

Author Comment

by:xpher
ID: 2633324
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2633348
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
 
LVL 1

Author Comment

by:xpher
ID: 2633353
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
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 2633384
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
 
LVL 1

Author Comment

by:xpher
ID: 2633407
Yes but I also have GIFImage in my uses.
This is Anders Melander? GIFimage which integrates into image component.

Cheers
Chris
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2633446
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
 
LVL 1

Author Comment

by:xpher
ID: 2633648
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2634200
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2634209
Correction: I tried v2.2
0
 
LVL 1

Author Comment

by:xpher
ID: 2635531
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
 
LVL 1

Expert Comment

by:nrico
ID: 2639890
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2640915
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
 
LVL 1

Author Comment

by:xpher
ID: 2646079
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now