Just been working with some code and saw this little gem.
try
{Some code}
except
showmessage('error');
end;
This is a good example of the developer being lazy and not being bothered to do something better. Can you imagine the user ringing up support and saying they are getting a 'error' message, it's not even with a capital 'E'.
There are a couple of issues I have with this bit of code. Firstly I have a rule that showmessage() is used while developing and should be removed when a deployment build is done, if a message needs to be displayed it should be in an message dialog (MessageDlg). The reason for this is that a message dialog looks better with icons specify what the message is, I use GExperts to create the code. Also, when developing I can quickly search for 'showmessage' and can see what messages need to be removed, this means there are no embarrassing debug messages displayed to the user.
Secondly is the message just says error, which it not helpful to the user or the developer when it is reported. The developer could have done something better by giving a more useful message like 'An error has occurred while...', with what the operation is doing. The developer could have also included the exception message like the following.
try
{Some code}
except
on e: exception do
MessageDlg('An error has occured in...' + e.Message, mtError, [mbOK], 0);
end;
Thursday, 18 July 2013
How NOT to handle exceptions
Labels:
Delphi,
error,
exception,
Handle,
messagedlg,
not,
showmessage
Friday, 5 July 2013
Delphi XE4 - The future of software development?
Delphi XE4 - The future of software development?: I have been working in software development for 15 years and have seen some changes, but nothing as much as what has happened recently. Over...
Monday, 1 July 2013
Delete files with a wildcard
Here is how to delete files with a wildcard. It requires SysUtls in the uses clause.
procedure DeleteFiles(FilePath, Wildcard: string);
var cSearchRec: TSearchRec;
iFind : integer;
sPath : string;
begin
sPath := IncludeTrailingPathDelimiter(FilePath);
iFind := FindFirst(sPath + Wildcard, faAnyFile, cSearchRec);
while iFind = 0 do
begin
DeleteFile(sPath + cSearchRec.Name);
iFind := FindNext(cSearchRec);
end;
FindClose(cSearchRec);
end;
In the while loop you can add additional conditions, like only files that have a specific prefix e.g.
if Pos(fPrefix, Copy(cSearchRec.Name, 0, Length(fPrefix))) > 0 then
procedure DeleteFiles(FilePath, Wildcard: string);
var cSearchRec: TSearchRec;
iFind : integer;
sPath : string;
begin
sPath := IncludeTrailingPathDelimiter(FilePath);
iFind := FindFirst(sPath + Wildcard, faAnyFile, cSearchRec);
while iFind = 0 do
begin
DeleteFile(sPath + cSearchRec.Name);
iFind := FindNext(cSearchRec);
end;
FindClose(cSearchRec);
end;
In the while loop you can add additional conditions, like only files that have a specific prefix e.g.
if Pos(fPrefix, Copy(cSearchRec.Name, 0, Length(fPrefix))) > 0 then
Subscribe to:
Posts (Atom)