Solved

Try and except syntax ?

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

Technology Partners: 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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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