procedure TForm1.Button2Click(Sender: TObject); var list:TStrings; begin list:=TStringlist.Create; list.Add('dfsd'); list.Add('dfsd'); list.Add('dfsd'); try list[4]; except On E: Exception do begin Application.MessageBox(Pchar(E.Message),'错误提示',0); end; end;end; 这是我刚测试的代码,还是不行啊!!看看有什么地方错了!!
和楼上的差不多 ... var lst_1: TStringList; begin lst_1 := TStringList.Create; try lst_1.Strings[2] := '2'; except on E: EStringListError do begin if Pos('List index out of bounds', E.Message) > 0 then ShowMessage('下标越界'); end; end; end;
except
On E: Exception do
begin
Application.MessageBox(Pchar(E.Message),'错误提示',0);
end;
end;
直接就像zxf_feng 说的一样,用if lr.Count>0 then判断一下
用try,不好意思的说,太磕碜了
var
list:TStrings;
begin
list:=TStringlist.Create;
list.Add('dfsd');
list.Add('dfsd');
list.Add('dfsd');
try
list[4];
except
On E: Exception do
begin
Application.MessageBox(Pchar(E.Message),'错误提示',0);
end;
end;end;
这是我刚测试的代码,还是不行啊!!看看有什么地方错了!!
...
var
lst_1: TStringList;
begin
lst_1 := TStringList.Create;
try
lst_1.Strings[2] := '2';
except
on E: EStringListError do
begin
if Pos('List index out of bounds', E.Message) > 0 then
ShowMessage('下标越界');
end;
end;
end;
list[4]不是一个函数或程式, 要不接收值,或赋值给其它变量
if 下标>high(i) then 越界了。
For Idx:=0 to List.Count-1 do
begin
.......
end;
你想干嘛呀,在list中增加了三个,它的index值0,1,2你偏要访问个4,何意呢,谁在写程序时会这样写呢?
在调试时你即便加了try也会弹出错误,你在执行exe文件时就会输出错误明细了.