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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Groan... Event Hangling Refuses to Work....

Status
Not open for further replies.

SpeedDemon

Programmer
Jan 1, 2001
70
GB
Hi all, im trying to catch conversion errors in delph using the try..except method.. eg:

try

...
except
on EZeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
on EMathError do HandleMathError;
else
HandleAllOthers;
end;


It works and catches my events, however any code that goes into the else bit does not work. Im trying to convert a string into an integer.

Am I missing something here? Where 'HandleAllOthers' is, can i put delphi code here or only other events?!?! Please help.

Cheers
 
Hmm.. no I think that should work. Here is an example :
===========================================
procedure TForm1.Button1Click(Sender: TObject);
var
t : string;
x : integer;

begin
try
t := 'test';
x := StrToInt(t);
except
on EZeroDivide do
showmessage('divide zero');
on EOverflow do
showmessage('overflow');
on EMathError do
showmessage('math error');
else
if HandleOthers then;

end;

end;
function TForm1.handleOthers : boolean;
var
x : integer;
begin
showmessage('Not handled');
// Can be any processing here
x := 6;
showmessage(IntToStr(x));
end;
===========================================

I think you will find this works fine. I made "HandleOthers" a function but it could easily have been a procedure.
 
Hi Oppenhiemer, thanks for the fast reply.

Where you have

else
if HandleOthers then;

Can I replace the 'if HandleOthers then' with code, or do I need to reference to a procedure like you have?

Cheers
 
Yes, just make sure that you enclose statements within a "begin .. end" block. So ..

procedure TForm1.Button1Click(Sender: TObject);
var
t : string;
x : integer;

begin
try
t := 'test';
x := StrToInt(t);
except
on EZeroDivide do
showmessage('divide zero');
on EOverflow do
showmessage('overflow');
on EMathError do
showmessage('math error');
else
begin
// This is where your code goes
end;

end;
 
Hi, thats exactly what I have done... Howevr nothing in the begin.. End is executed.

Let me show you my code...

Procedure CheckQuan(var itemindex:integer; quantity:string; prodindex:integer);
Begin
{Clear Old Values}
pinstock[1] := '';
pinstock[2] := '';

{Firstly Make Sure Entered String is valid integer}
try
tempquan := StrToInt(quantity);
except
on ECOnvertError do
begin
MessageDlg('You Must Enter An Integer Value',mtError,[mbOK],0);
{Cannot Convert, Set Flag to 1}
convgood := 1;
end;
else
begin
{Conversion Successful, Complete Quan Check}
{Make Sure Product Selected}

If prodindex = -1 then
begin
{Need to select a product}
emptyprod := 0;
MessageDlg('Select a Product',mtError,[mbOK],0);
end
else
begin
{Product Selected, Do a Quantity Search}
AssignFile(prodfile, prodfileurl);
Reset(prodfile);
seek(prodfile, prodindex);
read(prodfile, prodarray);
Closefile(prodfile);

{Next Check Quantity in stock}
if tempquan > prodarray.instock then
begin
{Not Enough Products In Stock}
pinstock[1] := IntToStr(prodarray.Instock);
pinstock[2] := '0';
MessageDlg(pinstock[1],mtError,[mbOK],0);
end
else
begin
{Enough in Stock.. Yippee!!}
pinstock[2] := '1';
MessageDlg(pinstock[2],mtError,[mbOK],0);
end;
end;
end; {End Else Here}
end;

end;
 
Hi, me again... i;ve been thinking, i may possibly have the wrong idea of how this try..except system works.

Im presuming that if the except condition is not met, it means that the conversion was a success ( think this is right :) )

Next, as this was done correctly, everything in the else part is run... Is the else part only run if a different evenhandler that was not listed in the except section is run?

Cheers
 
Hold on, lets just get it clear, it's confusing.

The TRY bit. If an error occurs in the TRY bit then it jumps to the EXCEPT bit.

In the EXCEPT bit, one can check for certain types of exceptions and run code appropriately.

The ELSE bit inside the EXCEPT will not be executed if the code was successful in the TRY bit.

TRY-FINALLYs, Code inside a FINALLY bit is ALWAYS executed, if an error occurs or not.

Eg
myobj := tmyobj.create;
TRY
TRY
StrToInt()
EXCEPT
ON ...
<failure code>
ELSE
<other failure code>
END; // of EXCEPT block
FINALLY
myobj.Free;
END;

Note, the Create for MyObj is outside the TRYs.

Does this make it clearer ?

lou [worm]
 
Your proc should be something like this. If the StrToInt fails, it will not execute the code after it that is still inside the TRY.

Procedure CheckQuan(var itemindex:integer; quantity:string; prodindex:integer);
Begin
TRY
{Clear Old Values}
pinstock[1] := '';
pinstock[2] := '';

{Firstly Make Sure Entered String is valid integer}
tempquan := StrToInt(quantity);
{Conversion Successful, Complete Quan Check}
{Make Sure Product Selected}

If prodindex = -1 then
begin
{Need to select a product}
emptyprod := 0;
MessageDlg('Select a Product',mtError,[mbOK],0);
end
else
begin
{Product Selected, Do a Quantity Search}
AssignFile(prodfile, prodfileurl);
Reset(prodfile);
seek(prodfile, prodindex);
read(prodfile, prodarray);
Closefile(prodfile);

{Next Check Quantity in stock}
if tempquan > prodarray.instock then
begin
{Not Enough Products In Stock}
pinstock[1] := IntToStr(prodarray.Instock);
pinstock[2] := '0';
MessageDlg(pinstock[1],mtError,[mbOK],0);
end
else
begin
{Enough in Stock.. Yippee!!}
pinstock[2] := '1';
MessageDlg(pinstock[2],mtError,[mbOK],0);
end;
end;
end;
EXCEPT
on ECOnvertError do
begin
MessageDlg('You Must Enter An Integer Value',mtError,[mbOK],0);
{Cannot Convert, Set Flag to 1}
convgood := 1;
end;

end;
 
thanks for the replies everyone.

Working a treat noW! Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top