在 try..except 语句中未捕获异常

时间:2021-03-08 11:41:34

标签: delphi

我有以下代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetFolderDate(Folder: string): TDateTime;
var
  Rec: TSearchRec;
  Found: Integer;
  Date: TDateTime;
  s:string;
begin
  Result := 0;
  if length(folder)=0 then exit;
  if Folder[Length(folder)] = '\' then
    Delete(Folder, Length(folder), 1);
  Found  := FindFirst(Folder, faDirectory, Rec);
  try
    try
    if Found = 0 then
    begin
      Date   := FileDateToDateTime(Rec.Time);
      Result := Date;
    end;
    except
      s:='error';
      form1.Caption:=s;
      //raise;
    end;
  finally
    FindClose(Rec);
  end;
end;

{ Sets the time for both files and directories }
{ for NT }

function NT_SetDateTime(FileName: string; dtCreation, dtLastAccessTime, dtLastWriteTime: TDateTime): Boolean;
// by Nicholas Robinson
var
  hDir: THandle;
  ftCreation: TFiletime;
  ftLastAccessTime: TFiletime;
  ftLastWriteTime: TFiletime;

  function DTtoFT(dt: TDateTime): TFiletime;
  var
    dwft: DWORD;
    ft: TFiletime;
  begin
    dwft := DateTimeToFileDate(dt);
    DosDateTimeToFileTime(LongRec(dwft).Hi, LongRec(dwft).Lo, ft);
    LocalFileTimeToFileTime(ft, Result);
  end;

begin
  hDir := CreateFile(PChar(FileName),
                     GENERIC_READ or GENERIC_WRITE,
                     0,
                     nil,
                     OPEN_EXISTING,
                     FILE_FLAG_BACKUP_SEMANTICS,
                     0);
  if hDir <> INVALID_HANDLE_VALUE then 
  begin
    try
      ftCreation       := DTtoFT(dtCreation);
      ftLastAccessTime := DTtoFT(dtLastAccessTime);
      ftLastWriteTime  := DTtoFT(dtLastWriteTime);
      Result := SetFileTime(hDir, @ftCreation, @ftLastAccessTime, @ftLastWriteTime);
    finally
      CloseHandle(hDir);
    end;
  end
  else
    Result := False;
end;

procedure getdir(dir1:string);
var sr:tsearchrec;
    sr2:tsearchrec;
    dtt,lastdate:tdatetime;
begin
  if Length(dir1)=0 then exit;
  if dir1[Length(dir1)] = '\' then
    Delete(dir1, Length(dir1), 1);
  if not directoryexists(dir1) then
    begin
    Form1.memo1.Lines.Add('dir "'+dir1+'" not exists');
    exit;
    end;
  if FindFirst(dir1+'\*.*',faDirectory,sr)<>0 then exit;
    repeat
    if sr.Attr and fadirectory=0 then continue;
    if (sr.Name='.') or (sr.Name='..') then continue;
    Form1.memo1.Lines.Add(dir1+'\'+sr.Name+'\');
    if sr.Attr and fadirectory=fadirectory then getdir(dir1+'\'+sr.name);
    lastdate:=0;
    if FindFirst(dir1+'\'+sr.name+'\*.*',faanyfile,sr2)<>0 then continue;
      repeat
      if (sr2.Name='.') or (sr2.Name='..') then continue;
      dtt:=getfolderdate(dir1+'\'+sr.name+'\'+sr2.Name);
      if lastdate=0 then lastdate:=dtt;
      if (lastdate<>0) and (dtt>lastdate) then lastdate:=dtt;
      Form1.memo1.Lines.Add('     '+sr2.Name+' '+datetimetostr(dtt));
      until findnext(sr2)<>0;
    findclose(sr2);
    Form1.memo1.Lines.Add('last date: '+datetimetostr(lastdate));
    if lastdate=0 then
       //lastdate:=getfolderdate(dir1+'\'+sr.name);
       continue;
    //NT_SetDateTime(dir1+'\'+sr.Name,lastdate,lastdate,lastdate);
    until FindNext(sr)<>0;
  FindClose(sr);
end;

procedure TForm1.Button1Click(Sender: TObject);
var dir1:string;
date:tdatetime;
begin
  dir1:=edit1.text;
  getdir(dir1);
end;

end.

目录有错误的日期,例如 1678 年(这是另一个错误的结果)

目录结构:

d:\test
d:\test\a
d:\test\a\1
d:\test\a\2 <wrong date here>
d:\test\a\3
d:\test\b
d:\test\b\1
d:\test\b\2
d:\test\b\3

它给出了一个错误:EConvertError with message 'Invalid argument to date encode'

如果我取消注释行,则不会发生错误并且不会执行异常

视频:https://www.youtube.com/watch?v=19FLQPbt53A

编辑:不重复。在 Delphi 7 上并不总是可以重现,在较新的版本上很难重现。可能是 IDE 错误。

0 个答案:

没有答案