Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP! Using try / catch statements! 2

Status
Not open for further replies.

Burtlancaster

Programmer
Jul 22, 2004
12
0
0
GB
Can someone please put me out of my misery! I cannot for the life work the following problem out.

Basically I would like to take an entered value from a TextBox (a string value) and convert it to a float! Simple! I know, but what happens if the user enters a non-digit expression (letters,symbols etc), using the "StrToFloat" method the program will simply return for example "Edit is not a valid floating point value." now what I want to do is catch this Expression and display my own message so I have tried the following code

Code:
//----------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//----------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//----------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//----------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
  {
  float EditText=StrToFloat(Edit->Text);
  Edit->Text=FloatToStrF(EditText,ffFixed,8,2);
  }
catch (...)
  {
  ShowMessage("non value entered");
  }
}
//----------------------------------------------

But the code will never see the catch instead whilst in debug it will display a EConvertError, and then say "Edit1 is not a valid floating point value." So how do I say hang on a minute if it cant convert it then do / say this...

Anyway I'll leave the problem in you capable hands.

Thanks for looking and any input would be much appreciated.
 
The try...catch statements are always overridden by debug. If you resume your program after debug catches a problem, your try...catch statement will execute.

Once you disable the debugging, the program will execute as expected.

James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
The other way is to validate the input into the text box as it is keyed!

use the KeyPress event to check that only numbers and 1 decimal point can be keyed...

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
if ( Key < '0' || Key > '9' )
{
if ( Key != '.' )
Key = 0;
else
if ( Edit1->Text.Pos(".") > 0 )
Key = 0;
}
}


Regards

BuilderSpec
 
you know its always the simple things that work the best, and your method BuilderSpec works a treat and I dont know why i didnt think of that myself. thanks.
 
The try/catch would probably work fine outside the debugger.
Sometimes I debug outside the debugger with message boxes and logging.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top