Link to home
Start Free TrialLog in
Avatar of Mark Brady
Mark BradyFlag for United States of America

asked on

Try and except syntax ?

I've got a simple proceedure that works great untilthe user deletes the numbers in an editbox.  The 'OnChange' event for the box runs this routine.
Can someone give me the correct syntax or re-write what I have written so my program doesn't crashout ?  Here is the error it produces...  'exception raised in exception class EConvertError with message 'Not a valid integer'

I know why it's happening just not how to fix it.

Here is the proceedure

procedure TForm1.edfeetChange(Sender: TObject);
var
lbtemp: integer;
lbtemp1: integer;
begin
sboff.down:=true;

lbtemp:= strtoint (edfeet.text)*12;
lbtemp1:= strtoint (edinches.text);
lbresult.caption:= inttostr (lbtemp + lbtemp1);

end;

thanks in advance

Mark
ASKER CERTIFIED SOLUTION
Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi elvin66,

there is old age "Val" procedure that do not raise an exception.

take a look at Delphi's help:
<<<

procedure Val(S; var V; var Code: Integer);

Description

Val converts the string value S to its numeric representation, as if it were read from a text file with Read.

S is a string-type expression; it must be a sequence of characters that form a signed real number.

V is an integer-type or real-type variable. If V is an integer-type variable, S must form a whole number.

Code is a variable of type Integer.

If the string is invalid, the index of the offending character is stored in Code; otherwise, Code is set to zero. For a null-terminated string, the error position returned in Code is one larger than the actual zero-based index of the character in error.

>>>

Hi ..

I assume that the user deletes the value of either edtFeet.Text or edtInches.text ... in which case, the error will be produces.  "Blank is not a valid integer value"

try the following

procedure TForm1.edfeetChange(Sender: TObject);
var
lbtemp: integer;
lbtemp1: integer;
begin
sboff.down:=true;
 if (edtFeet.Text = '') or (edtInches.Text = '') then
    begin
      showmessage('Enter a valid No');
      Exit;
    end
 else
    begin
      lbtemp:= strtoint (edfeet.text)*12;
      lbtemp1:= strtoint (edinches.text);
      lbresult.caption:= inttostr (lbtemp + lbtemp1);
    end;
end;

Regards
TAZI
Avatar of trex_fire
trex_fire

Let's take back Slayer code:

begin
 try
   lbTemp := StrToInt(edFeed.Text) * 12;
 except
   ShowMessage('Please enter a valid integer!');
   edFeed.Clear;
   edFeed.SetFocus;
//   exit;  dont need it
 end;
end;

This is clean code!! And Tazi, sorry but your code wont work if i enter 'A' in the box...