Delphi中如何捕获下标越界?请教大侠们??在线等待!!!

解决方案 »

  1.   

    当list没值的时我通过下标去访问,出现地址错误,也就是内存错误。
      

  2.   

    if lr.Count>0 then判断一下
      

  3.   

    那如何 用try来捕获异常呢!!
      

  4.   

    try
    except
        On E: Exception do
        begin
          Application.MessageBox(Pchar(E.Message),'错误提示',0);
        end;
    end;
      

  5.   

    这东西怎么还用try啊?
    直接就像zxf_feng 说的一样,用if lr.Count>0 then判断一下
    用try,不好意思的说,太磕碜了
      

  6.   

      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;
    这是我刚测试的代码,还是不行啊!!看看有什么地方错了!!
      

  7.   

    和楼上的差不多
    ...
    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;
      

  8.   


    list[4]不是一个函数或程式, 要不接收值,或赋值给其它变量
      

  9.   

    var i:array of integer;
    if 下标>high(i) then 越界了。
      

  10.   

    用COUNT去判断先,大于0再去取值
      

  11.   


    For Idx:=0 to List.Count-1 do
     begin
       .......
     end;
      

  12.   


    你想干嘛呀,在list中增加了三个,它的index值0,1,2你偏要访问个4,何意呢,谁在写程序时会这样写呢?
    在调试时你即便加了try也会弹出错误,你在执行exe文件时就会输出错误明细了.