* * *

Author Topic: on Exception : EConvertError  (Read 1032 times)

GregB

  • New member
  • *
  • Posts: 8
on Exception : EConvertError
« on: June 20, 2012, 11:28:30 am »
I am trying to read values from an array of variants, the values can be either float or string, so i try and read as a float and if i get an error then read it as a string.

I can read the float values but when i try to read a string i get the EConvertError x21 is an invalid float.
I thought that the on exception would handle this.

Can someone tell me what i am doing wrong?

Thanks.

this is the section that causes the error

Code: [Select]
      try
        sv := datap[row,col];
        fv := StrToFloat(sv);
        Write(f, 'f = ', fv, '     ');
        flush(f);
      except
        on Exception : EConvertError do
        begin
           ShowMessage(Exception.Message);
           Write(f, 's = ', sv, '     ');
        end;
      end;





This is the whole thing.

Code: [Select]
unit test_var_array;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;

type
  TForm1 = class(TForm)
  private
    { private declarations }
  public
    { public declarations }
  end;

  TDataArray = Array of Variant;
var
  Form1: TForm1;

  data : array of TDataArray;
  row, col : integer;



implementation

{$R *.lfm}

Procedure FillArray(var dataf : array of TDataArray);
var
  num : integer;
  str : string;

begin
  for row := 1 to 5 do
    for col := 1 to 5 do
    begin
      num := row + col;
      if (row Mod 2) = 0 then      {even row store it as string}
      begin
        str := IntToStr(row) + IntToStr(col);
        dataf[row,col] := 'x' + str;
      end
      else
        dataf[row,col] := row + col/10;      {odd row store it as float}
    end;
end;


Procedure PrintArray(var datap : array of TDataArray);
var
  f : TextFile;
  sv : string;
  fv : real;
  vv : variant;
begin
  Assign(f, 'C:\Test_Variant_array.txt');
  Rewrite(f);
  for row := 1 to 5 do
  begin
    for col := 1 to 5 do
    begin
      try
        sv := datap[row,col];
        fv := StrToFloat(sv);
        Write(f, 'f = ', fv, '     ');
        flush(f);
      except
        on Exception : EConvertError do
        begin
           ShowMessage(Exception.Message);
           Write(f, 's = ', sv, '     ');
        end;
      end;
    end;
    WriteLn(f,'');
  end;
  CloseFile(f);
end;


begin
  SetLength(data, 6,6);
  FillArray(data);
  PrintArray(data);
end.   

Leledumbo

  • Hero Member
  • *****
  • Posts: 4296
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: on Exception : EConvertError
« Reply #1 on: June 20, 2012, 11:32:50 am »
Quote
I can read the float values but when i try to read a string i get the EConvertError x21 is an invalid float.
x21 is an invalid float -> very damn clear message, isn't it?
Quote
I thought that the on exception would handle this.
Yes, the exception should handle this. However, if you're debugging from the IDE and you didn't register the exception to be ignored, the debugger with catch the exception first, prior to your exception handler.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: on Exception : EConvertError
« Reply #2 on: June 20, 2012, 12:01:57 pm »
Exception handling ideally should never really be used for controlling the flow of your application.

Use TryStrToFloat instead, this will return false if it can't convert.

GregB

  • New member
  • *
  • Posts: 8
Re: on Exception : EConvertError
« Reply #3 on: June 20, 2012, 12:54:05 pm »
Thanks Guys

Leledumbo ...
yep the error message was very clear :)
under environment - options - Debugger - Language Exceptions i had EconvertERROR checked but it still did not work.
waw that what you meant?

KpjComp ...
That worked well :)

I am muddling my way through Lazarus.

Leledumbo

  • Hero Member
  • *****
  • Posts: 4296
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: on Exception : EConvertError
« Reply #4 on: June 20, 2012, 02:08:00 pm »
Quote
under environment - options - Debugger - Language Exceptions i had EconvertERROR checked but it still did not work.
waw that what you meant?
Yes, but since it still doesn't work, that's the weird thing... I have no idea, mine is working fine.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads