如何解决TRY 。。。EXCEPT问题?

wzs 2000-09-06 10:55:00
我自己编一个函数判断是DATE有效时却不执行EXCEPT中的语句这是为何大侠帮我吧!!

函数:
function chk_date_valid(str:string):boolean;
var
TempDate: TDateTime;
begin
try
TempDate := StrToDateTime(str);
result:=true;
except
on EConvertError do
result:=false;
end;
end;

调用时:

if chk_date_valid(maskedit1.text) then ...{这时maskedit1.text=' - - '}或='2000-13-13'等等它只执行到tempdate:=strtodatetime(str)就报错而不跑到EXCEPT中去这是为何?
错误显示:
project rs_system.exe raised exception class Econverterror with message" - - "is not a valid date and time'.process stopped.use step or run to continue.

...全文
399 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzs 2000-09-06
  • 打赏
  • 举报
回复
附加一道题:
如何自动获取错误的代码及错误代码信息?就VB一样智能型的
在VB中只需要用它的ERR对象就能自动识别你程序错的原因(如:err.code就能获取错误的代码)
Sayhigh 2000-09-06
  • 打赏
  • 举报
回复
不会吧你在Except后面加一个Showmessage,把Onxxx注释一下,看看行不行
function chk_date_valid(str:string):boolean;
begin
try
result:=true;
StrToDateTime(str);
except
showmessage('Date Error');
result:=false;
end;
end;

JGTM2000 2000-09-06
  • 打赏
  • 举报
回复
ChDw的答案是对的,你脱离Delphi运行该程序就会执行到Exception里面。仔细看看Debugger的相关选项,很灵活的处理这类问题。另外,有些Exception类有ErrorCode和Message,你可以这样用:

try
//...
except
On ECE: EConvertError do
ShowMessage(ECE.ClassName+': '+ECE.Message);
On EWE: EWin32Error do
ShowMessageFmt('%s[%d]: %s',[EWE.ClassName,EWE.ErrorCode,ECE.Message);
end;
Pardo 2000-09-06
  • 打赏
  • 举报
回复
利用函数StrToDate()转换自身的错误捕捉

procedure TForm1.DateEditExit(Sender: TObject);
begin
//利用函数转换的错误捕捉来处理...
if DateEdit1.Text<>'' then
begin
try
StrToDate(DateEdit1.Text);//转换
except
DateEdit1.SetFocus;
MessageBeep(0);
raise Exception.Create('"'+DateEdit1.Text+'" 不是正确的日期格式!');
end{try};
DateEdit1.Text:=DateToStr(StrToDate(DateEdit1.Text));
end{if};
end;
sky__horse 2000-09-06
  • 打赏
  • 举报
回复
好像和"瘟豆柿"的日期格式有关,也许"瘟豆柿"的某个日期格式下你的写法可以用?
很久以前碰到过这个问题,不妨414
ChDw 2000-09-06
  • 打赏
  • 举报
回复
那个提示应该是Delphi调试过程中拦截下来的,你在Delphi的Language Exception中应该
可以找到Stop on Delphi Exception的选项,把它去掉就可以了
dragongong 2000-09-06
  • 打赏
  • 举报
回复
strtodatetime在sysutils中的说明:

function StrToDateTime(const S: string): TDateTime;
var
Pos: Integer;
Date, Time: TDateTime;
begin
Pos := 1;
Time := 0;
if not ScanDate(S, Pos, Date) or not ((Pos > Length(S)) or
ScanTime(S, Pos, Time)) then
begin // Try time only
Pos := 1;
if not ScanTime(S, Pos, Result) or (Pos <= Length(S)) then
ConvertErrorFmt(@SInvalidDateTime, [S]);
end else
if Date >= 0 then
Result := Date + Time else
Result := Date - Time;
end;
你可以直接使用scanDate, scantime检查

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧