如何捕捉错误,例如连接数据库登陆失败或想创建数据库已存在的数据库,错误都是提示Class EOleException,如何区分这两个错误呢,谢谢您的帮助
用下列语句不能区分:
try  except
    on EOleExceptiondo
      showmessage('HaHa');
    ...
  end;

解决方案 »

  1.   

    可以通过错误代码区分:
    try
      ……
    except
      on E:EOleException do                 
      begin
        case E.Errors[0].ErrorCode of
         xxx:showmessage();
         yyy:showmessage();
        end;
      end;
    end;
      

  2.   

    to guestman(天涯浪子):try
      ……
    except
      on E:EOleException do                 
      begin
        case E.Errors[0].ErrorCode of//这里出错,errors没定义
         xxx:showmessage();
         yyy:showmessage();
        end;
      end;
    end;
      

  3.   

    同意 guestman(天涯浪子) 应该行。
    有问题请发信息到我的E-mail:[email protected]
      

  4.   

    try
    //连接
    execpt
    //处理所有错误
    end
    这样肯定能捉到连接错误,管它是什么错误类型。
      

  5.   

    except
      on E:EOleException do                 
      begin
        case E.ErrorsCode of
         xxx:showmessage();
         yyy:showmessage();
        end;
      end;
    end;
      

  6.   

    D5上测试通过!
    procedure TForm1.Button1Click(Sender: TObject);
    var
      e:EOleException;
    begin
      try
        ADOConnection1.Connected:=true;
      except
        on E:EOleException do
        begin
          case E.ErrorCode of
           -2147467259:showmessage('未发现数据源!');
           -2147217843:showmessage('无效的登录名/口令');
           else
             showmessage('错误代码:'+inttostr(E.ErrorCode));
          end;
        end;
      end;
    end;