delphi try except语句 和 try finally语句用法以及区别

1、当执行try...except之间的语句序列没有发生异常时,则忽略异常处理部分的语句,程序直接转移try...except..finally后面的语句处执行;
2、Except括起来的语句,则只有在产生异常的情况下会被执行,其他情况一概不执行的。 
3、Finally括起来的语句是铁定会被执行的,无论是否有异常产生; 

try//尝试执行
{SomeCode} 
except//出错的时候执行, Except有特定的错误类型 
{SomeCode} 
end; 

try//尝试执行
{SomeCode} 
finally//无论如何都强制执行
{SomeCode} 
end; 

例:
try
Age:=StrToInt(Edit1.Text);
ShowMessage(Format('生于%d年',[YearOf(Now)-Age]));
except
on EConvertError do
showmessage('输入编辑框的不是一个有效的数字!');
on ERangeError do
showmessage('输入编辑框的年龄值太大!');
end;



保留字on…do用于判断异常类型。必须注意的是:except后面的语句必须包含在某一个on…do模块中,而不能单独存在。这又是同finally不同的一个地方。


异常处理
try
//成功失败都跳进finally语句里执行
finally

end;
try
//失败才跳进 except里执行
except on e:Exception do
ShowMessage(e.Message);
end; 

 

例子:我写的一个服务

procedure TDBMonitorService.Timer1Timer(Sender: TObject);
var
tSQL: string;
tSAMPLEID,tJG,tCLASSES,tGUID,tTESTTIME,tC,tSi,tMn,tp,tS,tCu,tNi,tCR,tAS,tSN,tPB,tV,tAL,tTI,tSB,tBI,tMO, tLCH,tLGH,tQX,tSAMPLE,sSCBZ:string;
MyArr: array[0..4] of string;
i, nFH,j,k:Integer;
begin
  Ansys := true;
  try
    //SYN_SIGN=1 已上报 SYN_SIGN=0 未上报
      try
        with qry_erp do
        begin
          if Active then Close;
          SQL.Clear;
          SQL.Add('SELECT SAMPLEID, SYN_SIGN, nvl(FD_C,0) FD_C, nvl(FD_SI,0) FD_SI, nvl(FD_MN,0) FD_MN, nvl(FD_P,0) FD_P,');
          SQL.Add('nvl(FD_S,0) FD_S, nvl(FD_CU,0) FD_CU, nvl(FD_NI,0) FD_NI, nvl(FD_CR,0) FD_CR, nvl(FD_AS,0) FD_AS, nvl(FD_SN,0) FD_SN,');
          SQL.Add('nvl(FD_PB,0) FD_PB, nvl(FD_V,0) FD_V, nvl(FD_AL,0) FD_AL, nvl(FD_TI,0) FD_TI, nvl(FD_SB,0) FD_SB, nvl(FD_BI,0) FD_BI, nvl(FD_MO,0)           FD_MO, SAMPLEOK, TESTTIME ');
          SQL.Add('FROM D1_FDSYTS_DATA WHERE SYN_SIGN=0 ORDER BY SENDTIME DESC');
          Open;
        end;

      except
        on e:Exception do
          begin
            WriteLog(e.Message+'->数据库打开错误!');
            Exit;
          end;
      end;

    if (not qry_erp.IsEmpty) then
    begin
      qry_erp.First;
      try
        while not qry_erp.Eof do
        begin
          tSQL := '';
          sSCBZ:='';
          tSAMPLEID := qry_erp.FieldByName('SAMPLEID').AsString; // 炉次号 ,炉罐号 ,去向(9-17-1234,123,A)
          tSAMPLE :=tSAMPLEID;
          j:=1;
          k:=0;
          for i:=0 to 2 do
          begin
            nFH:=pos(',',tSAMPLE);
            k:=k+nFH;
            if(nFH<>0) then
            begin
              if(j=1) then
              begin
                tLCH:=Trim(copy(tSAMPLEID,0,k-1));
              end;
              if(j=2)then
              begin
                tLGH:=Trim(copy(tSAMPLE,0,pos(',',tSAMPLE)-1));
                tQX:=copy(tSAMPLE,nFH+1,length(tSAMPLE));
              end;
              tSAMPLE:=copy(tSAMPLE,nFH+1,length(tSAMPLE));
              j:=j+1;
            end
            else if(nFH=0) then break;
          end;

          if(j=3)then
          begin
            tC := qry_erp.FieldByName('FD_C').AsString ;
            tSi := qry_erp.FieldByName('FD_SI').AsString;
            tMn := qry_erp.FieldByName('FD_MN').AsString;
            tp := qry_erp.FieldByName('FD_P').AsString;
            tS := qry_erp.FieldByName('FD_S').AsString;
            tCu:= qry_erp.FieldByName('FD_CU').AsString;
            tNi:= qry_erp.FieldByName('FD_NI').AsString;
            tCR := qry_erp.FieldByName('FD_CR').AsString;
            tAS := qry_erp.FieldByName('FD_AS').AsString;
            tSN := qry_erp.FieldByName('FD_SN').AsString;
            tPB := qry_erp.FieldByName('FD_PB').AsString;
            tV := qry_erp.FieldByName('FD_V').AsString;
            tAL:= qry_erp.FieldByName('FD_AL').AsString;
            tTI:= qry_erp.FieldByName('FD_TI').AsString;
            tSB:= qry_erp.FieldByName('FD_SB').AsString;
            tBI:= qry_erp.FieldByName('FD_BI').AsString;
            tMO:= qry_erp.FieldByName('FD_MO').AsString;
            tJG:= qry_erp.FieldByName('SAMPLEOK').AsString;
            tTESTTIME:= qry_erp.FieldByName('TESTTIME').AsString; //铁水化验时间

            tCLASSES:='';

            MyArr[0]:='';
            MyArr[1]:='';
            MyArr[2]:='';
            MyArr[3]:='';
            MyArr[4]:='';
            if((tC<>'') and (tSi<>'') and (tS<>'') and (tp<>'') and (tMn<>'') ) then
            begin
              if( (StrToFloat(tC)>=3.5) and (StrToFloat(tSi)<=0.8) and (StrToFloat(tS)<=0.05) and (StrToFloat(tp)<=0.17) and (StrToFloat(tMn)<=1)) then
              begin
                MyArr[0]:='A';
              end;
            end;

            if((tC<>'') and (tSi<>'') and (tS<>'') and (tp<>'') and (tMn<>'') and (tCR<>'') and (tNi<>'') and (tCu<>'') and (tMO<>'') and (tAS<>'')) then
            begin
               if( (StrToFloat(tC)>=3.5) and (StrToFloat(tSi)>=0.2) and (StrToFloat(tSi)<=0.6) and (StrToFloat(tS)<=0.05) and (StrToFloat(tp)<=0.14) and

              StrToFloat(tMn)<=1) and (StrToFloat(tCR)<=0.10) and (StrToFloat(tNi)<=0.10) and (StrToFloat(tCu)<=0.05) and (StrToFloat(tMO)<=0.05) and               (StrToFloat(tAS)<=0.008) ) then
              begin
                MyArr[1]:='B';
              end;
            end;
            if((tC<>'') and (tSi<>'') and (tS<>'') and (tp<>'') and (tMn<>'') and (tCR<>'') and (tNi<>'') and (tCu<>'') and (tMO<>'') and (tAS<>'') and (tPB<>'') and (tSN<>'') and             (tSB<>'') and (tBI<>'') and (tTI<>'')) then
            begin
              if( (StrToFloat(tC)>=3.5) and (StrToFloat(tSi)>=0.2) and (StrToFloat(tSi)<=0.6)
              and (StrToFloat(tS)<=0.05) and (StrToFloat(tp)<=0.14) and (StrToFloat(tMn)<=1)
              and (StrToFloat(tCR)<=0.10) and (StrToFloat(tNi)<=0.10) and (StrToFloat(tCu)<=0.05)
              and (StrToFloat(tMO)<=0.05) and (StrToFloat(tAS)<=0.008) and (StrToFloat(tPB)<=0.007)
              and (StrToFloat(tSN)<=0.005) and (StrToFloat(tSB)<=0.005) and (StrToFloat(tBI)<=0.007)
              and (StrToFloat(tTI)<=0.050) ) then
              begin
                MyArr[2]:='C';
              end;
            end;
            if((tC<>'') and (tSi<>'') and (tS<>'') and (tp<>'') and (tMn<>'') and (tCR<>'') and (tNi<>'') and (tCu<>'') and (tMO<>'') and (tAS<>'') and (tPB<>'') and (tSN<>'') and             (tSB<>'') and (tBI<>'') and (tTI<>'')) then
            begin
              if( (StrToFloat(tC)>=3.5) and (StrToFloat(tSi)>=0.2) and (StrToFloat(tSi)<=0.6)
              and (StrToFloat(tS)<=0.05) and (StrToFloat(tp)<=0.14) and (StrToFloat(tMn)<=1)
              and (StrToFloat(tCR)<=0.05) and (StrToFloat(tNi)<=0.05) and (StrToFloat(tCu)<=0.05)
              and (StrToFloat(tMO)<=0.05) and (StrToFloat(tAS)<=0.005) and (StrToFloat(tPB)<=0.005)
              and (StrToFloat(tSN)<=0.003) and (StrToFloat(tSB)<=0.003) and (StrToFloat(tBI)<=0.005)
              and (StrToFloat(tTI)<=0.050) ) then
              begin
                MyArr[3]:='D';
              end;
            end;
            if((tC<>'') and (tSi<>'') and (tS<>'') and (tp<>'') and (tMn<>'') and (tCR<>'') and (tNi<>'') and (tCu<>'') and (tMO<>'') and (tAS<>'') and (tSN<>'') and (tTI<>'')) then
            begin
              if( (StrToFloat(tC)>=3.5) and (StrToFloat(tSi)>=0.2) and (StrToFloat(tSi)<=0.6)
              and (StrToFloat(tS)<=0.05) and (StrToFloat(tp)<=0.14) and (StrToFloat(tMn)<=1)
              and (StrToFloat(tCR)<=0.03) and (StrToFloat(tNi)<=0.03) and (StrToFloat(tCu)<=0.03)
              and (StrToFloat(tMO)<=0.03) and (StrToFloat(tAS)<=0.006) and (StrToFloat(tSN)<=0.007)
              and (StrToFloat(tTI)<=0.050) ) then
              begin
                MyArr[4]:='E';
              end;
            end;

            for i:=0 to 4 do
            begin
              if MyArr[i] <>'' then
                tCLASSES:=tCLASSES+ MyArr[i]+',';
            end;
            tCLASSES:=copy(tCLASSES,1,length(tCLASSES)-1);

            with qry_scyx do
            begin
              if Active then Close;
              SQL.Clear;
              SQL.Add('SELECT * FROM SJCJ_GLTSHY_TEST where LCH='''+tLCH+''' and LGH='''+tLGH+''' and yxbz=''1'' ');
              Open;
            end;

            if qry_scyx.IsEmpty then
            begin
              tSQL := 'insert into            SJCJ_GLTSHY_TEST(BH,LCH,LGH,C,SI,MN,S,P,JG,TSHYJLSJ,V,TI,CR,NI,PB,SN,AS_,SB,BI,MO,CU,CR2,NI2,PB2,SN2,AS_2,SB2,BI2,MO2,CU2,TI2,V2,CLAS              SES,yxbz,qx,CJR)'
              +' values(sys_guid(),'''+tLCH+''','''+tLGH+''','''+tC+''','''+tSi+''','''+tMn+''','''+tS+''','''+tp+''','''+tJG+''',to_date('''+tTESTTIME+''',''yyyy-mm-dd hh24:mi:ss'') '
              +' ,'''+tV+''','''+tTI+''','''+tCR+''','''+tNi+''','''+tPB+''','''+tSN+''','''+tAS+''','''+tSB+''','''+tBI+''','''+tMO+''','''+tCu+''' '
              +' ,'''+tCR+''','''+tNi+''','''+tPB+''','''+tSN+''','''+tAS+''','''+tSB+''','''+tBI+''','''+tMO+''','''+tCu+''','''+tTI+''','''+tV+''' ,'''+tCLASSES+''',''1'','''+tQX+''',''FDSY'')';
              writelog('Insert: '+tSQL);
            end
            else
            begin
            //如果有数据则更新
            tGUID := qry_scyx.FieldByName('BH').AsString;
            tSQL := ' update SJCJ_GLTSHY_TEST set C='''+tC+''',SI='''+tSi+''',MN='''+tMn+''',S='''+tS+''',P='''+tp+''',JG= '''+tJG+''', TSHYJLSJ=to_date('''+tTESTTIME+''',''yyyy-mm-dd hh24:mi:ss'') '
            +' ,V='''+tV+''',TI='''+tTI+''' ,CR='''+tCR+''',NI='''+tNi+''',PB='''+tPB+''',SN='''+tSN+''',AS_='''+tAS+''',SB='''+tSB+''' '
            +' ,BI='''+tBI+''',MO='''+tMO+''',CU='''+tCu+''',CR2='''+tCR+''',NI2='''+tNi+''',PB2='''+tPB+''',SN2='''+tSN+''',AS_2='''+tAS+''',SB2='''+tSB+''' '
            +' , BI2='''+tBI+''',MO2='''+tMO+''',CU2='''+tCu+''',TI2='''+tTI+''',V2='''+tV+''',CLASSES='''+tCLASSES+''' '
            +' where BH='''+tGUID+''' ';
            writelog('Update: '+tSQL);
          end;
          sSCBZ:='1';
          end
          else begin
            sSCBZ:='2';
          end;

          if (sSCBZ<>'') then
          begin
          try
            if (tSQL<>'') then
          begin
            with qry_scyx do
            begin
              if Active then Close;
              SQL.Text := tSQL;
              ExecSQL;
            end;
          end;
        except
          on e:Exception do
          begin
            WriteLog('执行oracle语句时:'+e.Message+';SQL语句:'+tSQL);        
            qry_erp.Next;
            Continue;
            WriteLog('下一条记录');
          end;
      end;
      try
        //更新Accesss
        qry_erp.Edit;
        qry_erp.FieldByName('SYN_SIGN').AsString := sSCBZ; //更新已上报标志
        except
          on e:Exception do
          WriteLog('更新Access上报标志时 : '+e.Message);
        end;
     end;
     qry_erp.Next;
    end;
    except
    on e:Exception do
      WriteLog('线程执行循环体:'+e.Message);
    end;
  end;

  finally
    Ansys := false;
  end;
end;

转载于:https://www.cnblogs.com/michellexiaoqi/p/6984269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值