Board index » delphi » invalid floating-point operation

invalid floating-point operation

Hello,

I am trying to make a program to calculate some thing, therefore I need to
read values from edit-boxes, and then calculate with it.
so I did this.

L:=StrToFloat(form1.LEdit.Text);

This workes.

But now I want to give a error message when someone puts text in this
edit-box instead of a number, because now my program crashes when it wants
to read the edit-box.

I think it should look something like this:

Try
........
except
.........
end;

but if I put "L:=StrToFloat(form1.LEdit.Text);'' in the Try part then the
program still crashes.

Do you have an answer?

Thanks!             Douwe

 

Re:invalid floating-point operation


D. Nakken <d.nak...@student.tudelft.nl> skrev i en
nyhedsmeddelelse:807a17$cn...@news.tudelft.nl...

Quote
> I think it should look something like this:

> Try
> ........
> except
> .........
> end;

> but if I put "L:=StrToFloat(form1.LEdit.Text);'' in the Try part then the
> program still crashes.

I don't think it crashes.
When you use the de{*word*81} it will show you when an exception occurs.
That is not a crash.

Re:invalid floating-point operation


In article <807a17$cn...@news.tudelft.nl>,
  "D. Nakken" <d.nak...@student.tudelft.nl> wrote:
Quote
> Hello,

> I am trying to make a program to calculate some thing, therefore I
need to
> read values from edit-boxes, and then calculate with it.
> so I did this.

> L:=StrToFloat(form1.LEdit.Text);

> This workes.

> But now I want to give a error message when someone puts text in this
> edit-box instead of a number, because now my program crashes when it
wants
> to read the edit-box.

> I think it should look something like this:

> Try
> ........
> except
> .........
> end;

> but if I put "L:=StrToFloat(form1.LEdit.Text);'' in the Try part then
the
> program still crashes.

> Do you have an answer?

> Thanks!             Douwe

It is not that the program "crashes", it is only doing exactly what I
would expect.  If you want the program to execute the except clause
without going to the de{*word*81} when the exception occurs you will need to
turn off "break on exception" in the project settings.  The system
however will then not break on ANY exceptions.  There was a posting a
couple of weeks ago about how to turn "break on exception" "off" from
your code, but none of the answers were a neat solution, I was hoping to
see something like {BREAK_ON_EXCEPTION OFF} just like the directive
{WARNINGS OFF} but the IDE does not support this :-(.  If you just step
to the next line, the de{*word*81} will execute your first line of code in
the except clause and you can continue to run or step as desired.  If
you run your program from outside the IDE you will see that it will trap
the errors seamlessly, just as you would expect it to.  I know it is
annoying, but even with try blocks you cannot seem to stop Delphi from
telling you about the error and THEN doing what error handling you have
written :-(

Trevor

Sent via Deja.com http://www.deja.com/
Before you buy.

Re:invalid floating-point operation


D. Nakken <d.nak...@student.tudelft.nl> a crit dans le message :
807a17$cn...@news.tudelft.nl...

Quote
> Hello,

> I am trying to make a program to calculate some thing, therefore I need to
> read values from edit-boxes, and then calculate with it.
> so I did this.

> L:=StrToFloat(form1.LEdit.Text);

> This workes.

Try another way and use VAL, which gives a code where the error is.
If the string length is greater than zero and code is zero, then you have a
correct value.
Look in the help for VAL.

uses Dialogs;
var

  I, Code: Integer;
begin
  { Get text from TEdit control }
  Val(Edit1.Text, I, Code);
  { Error during conversion to integer? }
  if code <> 0 then
    MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk],
0);
  else
    Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));
  Readln;
end;

Quote
> But now I want to give a error message when someone puts text in this
> edit-box instead of a number, because now my program crashes when it wants
> to read the edit-box.

> I think it should look something like this:

> Try
> ........
> except
> .........
> end;

> but if I put "L:=StrToFloat(form1.LEdit.Text);'' in the Try part then the
> program still crashes.

> Do you have an answer?

> Thanks!             Douwe

Other Threads