var Form1: TForm1; a,b,c,x1,x2,d: real; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin a:=StrToFloat(Edit1.Text); b:=StrToFloat(Edit1.Text) ; c:=StrToFloat(Edit1.Text) ; d:=sqr(b)-4*a*c; if d>0 then begin try x1:=(-b+sqrt(d))/(2*a); x2:=(-b-sqrt(d))/(2*a); except on EzeroDivide do MessageDLG('Ділення на 0!',mtInformation,[mbOK],0); end; ShowMessage('x1='+FloatToStr(x1)+',x2='+FloatToStr(x2)); end else if d=0 then begin try x1:=(-b)/(2*a); //здесь вылетает ошибка Invalid floating point operation except on EzeroDivide do MessageDLG('Ділення на 0!',mtInformation,[mbOK],0); end; ShowMessage('x='+FloatToStr(x1)); end else if d<0 then begin try x1:=(-b+sqrt(abs(d)))/(2*a); x2:=(-b-sqrt(abs(d)))/(2*a); except on EzeroDivide do MessageDLG('Ділення на 0!',mtInformation,[mbOK],0); end; ShowMessage('x1='+FloatToStr(x1)+',x2='+FloatToStr(x2)); end end; end. 

So, tell me, please, what is wrong and how to deal with it?

  • the error pops up exactly when zero falls into the variable (a)! in other cases, the program works correctly. try, should it handle right? - Zein

2 answers 2

Try

...

Except

They do not work under IDE, that is, if Delphi is open, an error will crash, but run the program not from Delphi and the error will not crash.

  • did not help .. - Zein
  • In the sense did not help? - SoftR
  • the error remained .. maybe I didn’t understand it "but take start the program not from Delphi and the error will not take off." I just ran the executable. If I misunderstood, then the question is: how can it not work if this is a task for the curriculum? And I will add that I use Delphi 7 - Zein
  • Yes, he took your example, compiled it in delphi7, really when entering 0 in Edit1 crashes. I'm digging now. - SoftR
  • one
    In general, your mistake is that abc are equal and if a = 0 and b = 0, then it turns out that (-0) (2 * 0) = 0 \ 0, but 0 divided by 0 is infinity :) And therefore not an error related to division by zero is a mistake so that the number is too large. It is enough to put a number other than 0 instead of b, and the trap works with joy with the message that division by zero occurs. So you can pat the brain of a teacher by forcing him to catch the error of division by zero :) if both arguments are 0 - SoftR
 ... try x1:=(-b)/(2*a); //здесь вылетает ошибка Invalid floating point operation except MessageDLG('Ділення на 0!',mtInformation,[mbOK],0); end; ShowMessage('x='+FloatToStr(x1)); ...