Solved

Try and except syntax ?

Posted on 2002-04-11
4
209 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:Mark Brady
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 50 total points
ID: 6935736
two examples:

begin
  lbTemp := StrToIntDef(edFeed.Text, 0) * 12;
  lbTemp1 := StrToIntDef(edInches.Text, 0);
  lblResult.Caption := IntToStr(lbTemp + lbTemp1);
end;

with this example, if it is not a valid number, you will get a zero

Another example

begin
  try
    lbTemp := StrToInt(edFeed) * 12;
  except
    ShowMessage('Please enter a valid integer!');
    edFeed.SetFocus;
    exit;
  end;
end;
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6936411
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.

>>>

0
 
LVL 1

Expert Comment

by:TAZI
ID: 6936728
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
0
 
LVL 1

Expert Comment

by:trex_fire
ID: 6938488
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...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi: ForceDirectory plain function 7 80
Performance of SQL statement 37 144
When i run adoquery my application freezes 26 206
DBGrid or StringGrid ? 6 133
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question