异常机制与屏蔽调试代码

procedure TForm1.Button3Click(Sender: TObject);
var
    Ratio, Numerator, Denominator: integer;
begin
    Denominator:=0;
    Numerator:=0;
    try
        Ratio:= Numerator div Denominator;
    except
        on EDivByZero do
        begin
            Ratio:=0; // 可以加入自己的ShowMessage
            {$IFOPT D-} // 如果变成D+,就会执行下面那句话。
            ShowMessage('0000');
            {$ENDIF}
        end;
    end;
    ShowMessage(IntToStr(Ratio));
end;

 

procedure TForm1.Button4Click(Sender: TObject);
var
    Ratio, Numerator, Denominator: integer;
begin
    Denominator:=0;
    Numerator:=0;
    Ratio:= Numerator div Denominator; // 程序虽然不会崩溃,但Delphi自己弹出DivByZero的英文错误,应该换成自己的
    ShowMessage(IntToStr(Ratio));
end;

 

你可能感兴趣的:(异常机制)