Board index » delphi » Can't trap EConvertError with StrToFloat..?!?

Can't trap EConvertError with StrToFloat..?!?

Hi there.

I have a function, which tests the validity of a string representation of a
float number.

But it doesn't work...:( If the 'test'-string is not a valid float, the
function always stops with an exception. Now, I understand that
the following code should trap the exception just nicely and return
me either true or false depending on the validity of the value.

What is going on here?

Funtion IsValidFloat(Test: String): Boolean;
var
   Temp : Real;

begin
      try
         Temp := StrToFloat(Test);
         Result:=True;
      except
        on EConvertError do
                Result:=False;
      end;
end;

Oh, I use Delphi 1.02 Client/Server. And no, I don't have the "Break on
exception" selected in the environment options. Actually, this happens also
when running the EXE by itself.

Or is there some simpler way to determine the validity of a float in string?

Peace,
                - Petri -

 

Re:Can't trap EConvertError with StrToFloat..?!?


In article <5aflj8$...@leivo.inet.fi>,
   petri.riipi...@tele.fi (Petri J. Riipinen) wrote:
]-Hi there.
]-
]-I have a function, which tests the validity of a string representation of a
]-float number.
]-
]-But it doesn't work...:( If the 'test'-string is not a valid float, the
]-function always stops with an exception. Now, I understand that
]-the following code should trap the exception just nicely and return
]-me either true or false depending on the validity of the value.
]-
]-What is going on here?

what kind of exception are you getting?  I tested your function,
and I could not reproduce the error...

  TestMe := 'This is not a number';
  if IsValidFloat(TestMe) then
    MessageDlg('[' + TestMe + '] is okay', mtInformation, [mbOK], 0)
  else
    MessageDlg('[' + TestMe + '] is INVALID', mtError, [mbOK], 0);

traps the error correctly everytime.  I can however force an exception
by passing a bad function parameter...say TestMe is an uninitialized
string pointer...then IsValidFloat(TestMe^) blows up, but the exception
is a GPF, not an EConvertError.

Mark Vaughan

]-
]-Funtion IsValidFloat(Test: String): Boolean;
]-var
]-   Temp : Real;
]-
]-begin
]-      try
]-         Temp := StrToFloat(Test);
]-         Result:=True;
]-      except
]-        on EConvertError do
]-                Result:=False;
]-      end;
]-end;
]-
]-Oh, I use Delphi 1.02 Client/Server. And no, I don't have the "Break on
]-exception" selected in the environment options. Actually, this happens also
]-when running the EXE by itself.
]-
]-Or is there some simpler way to determine the validity of a float in string?
]-
]-Peace,
]-                - Petri -

Re:Can't trap EConvertError with StrToFloat..?!?


petri.riipi...@tele.fi (Petri J. Riipinen) wrote:

Quote
>Hi there.
>I have a function, which tests the validity of a string representation of a
>float number.
>But it doesn't work...:( If the 'test'-string is not a valid float, the
>function always stops with an exception. Now, I understand that
>the following code should trap the exception just nicely and return
>me either true or false depending on the validity of the value.

I suppose you are testing your fuction from within Delphi ? If so,
then Delphi will stop execution and produce the error. If you compile
first and then run the exe it should work. I tested your function
(actually it's almost the same as the one I use) and it works fine
from the exe for me.

Other Threads