Solved

Try and except syntax ?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

15 Experts available now in Live!

Get 1:1 Help Now