1

我知道如何进行基本的异常处理。所以我可以使用“try except”方法在除以零时发出消息。

我想做的是,找到导致此错误的变量,然后在运行时更改其值。

例如:

procedure Calculate();
var
  a, b, c : Double;
begin
  try
    a := 4; //suppose i take this value from user and he enters 4
    b := 0; //suppose i take this value from user and he enters 0
    c := a/b;
    ShowMessage(FloatToStr(c));
  except
    on E : EZeroDivide do
    begin
      ShowMessage('Exception message = '+E.Message);

//i am not sure how to identify that its variable 'b' that is causing the error and has to be changed by a default value

      get(E....errorVaraiable);
      E....errorVaraiable := 0.00001;
      c := a/E....errorVariable;

      ShowMessage(FloatToStr(c));
    end;
  end;

拜托,谁能帮我解决这个问题?

4

1 回答 1

2

这是您的示例的修改版本,可以满足您的需求。

procedure Calculate();
var
  a, b, c : Double;
begin
  a := 4; //suppose i take this value from user and he enters 4
  b := 0; //suppose i take this value from user and he enters 0
  if IsZero(b) then
  begin
    ShowMessage('b cannot be 0')
  end
  else 
  begin
    c := a/b;
    ShowMessage(FloatToStr(c));
  end; 
end;
于 2013-05-24T13:09:49.997 回答