Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LoadFromFile and SaveToFile exceptions

Status
Not open for further replies.

bobbie100

Programmer
Aug 29, 2003
64
0
0
GB
I want to add appropriate exception handling with useful messages when loading and saving various descendants of TGraphic using the LoadFromFile and SaveToFile procedures.

The files are selected using the standard Delphi OpenDialog (for LoadFromFile) and SaveDialog (for SaveToFile).

What exceptions do I need to handle? I have been reading the help files and other sources, but am having difficulty deciding which are the most appropriate exceptions to catch. Maybe I should be using the EFCreateError, EFOpenError, or more generic EFileStreamError exceptions, or maybe EInOutError?

Can anybody advise on what I should be putting at ??? in:

Code:
try
  LoadFromFile(FileName);
except
  ???
end;

try
  SaveToFile(FileName);
except
  ???
end;

I am using Delphi 7. Backwards compatibility with earlier versions of Delphi is not required.

Many thanks for spending the time to read this (and even more thanks if you reply!)
 
i may be wrong but you dont have to catch any if you dont want to, just put a message on except.
if im wrong then hopefully someone else will post and teach both of us.

try
LoadFromFile(FileName);
except
showmsg('error opening file');
end;

p.s. i cant remember if its showmessage or showmsg.

Aaron Taylor
John Mutch Electronics
 
Its showmessage or you can use msgdialog('prompt',messagetype,buttons, helpid)
which can give your users options as to waht to do if the file does not load correctly, e.g set some defaults, alow a change of file to load, or just give up!.
The syntax seems ok to me you are handeling the exception, even this does it, frowned on though.

try
action
except
end;



Steve
Be excellent to each other and Party on!
 
Actually, it's MessageDlg not MsgDialog:
Code:
MessageDlg(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint)


Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thanks guys, but my emphasis was on generating useful messages instead of the annoying "failed" with no hint what caused it. For instance failures to save a file might include file access problems, or problems during the write process (e.g. disk full).

Any ideas what exceptions to catch to achieve this type of depth? There must be some of you out there that write user friendly code!

Bob
 
**if label1 is ready to show error**



try
LoadFromFile(FileName);
except
on err1:exception1 do
label1.caption:='message1';
on err2:exception2 do
label1.caption:='message2';
end;


or******

try
LoadFromFile(FileName);
except
on err:exception do
label1.caption:=err.message;
end;

 
Much as we all like Bill and co, I doubt if SysErrorMessage will return very useful messages for users?
GetLast error will return the last windows error code (a number)

Is what you need to know is all the possible errors for file handling, there are lots of constants defined for this in the windows.pas file then you can trap the error code and write your meaningful messages. (most of the useful codes are < 100).

Also what I am saying is don't rely on the system to give you a meaningful message or for us to suggest them, you need to test your error handling and make your messages fit your applications target users.

This shouldnt effect simple loading and saving, but in some cases it might not be what you expect.
for example the Findnext function should return error codes,
but in some circumstances if it encounters a file which the current user has no access, Findnext returns 0 'no error' but checking 'getlasterror' reveals that error 5 'Access denied' has occurred and no exception was raised.




Steve
Be excellent to each other and Party on!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top