Solved

Try and except syntax ?

Posted on 2002-04-11
4
204 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now