View Full Version : try... except
Fernando
19-05-2003, 12:28 PM
Hi guys.
Is there a way to do this?:
////////////////////////////////////////
try
if MessageDlg('Press Yes or No', mtConfirmation, [MBYES, MBNO], 0) = MRYES then GOtoEXCEPTend;
except
ShowMessage('The user clicked on YES button.');
end;
////////////////////////////////////////
Thank you.
TheLion
19-05-2003, 02:00 PM
You can leave out the Try... Except there ... since the MessageDlg is unlikely to fail
if MessageDlg('Press Yes or No', mtConfirmation, [MBYES, MBNO], 0) = MRYES then
Begin
ShowMessage('The user clicked on YES button.');
End
Else
Begin
ShowMessage('The user clicked the NO button.');
End;
It might be that I misunderstood what you where trying to do, however I don't see the need for try...except in this scenario... If you want to jump from the loop if the user presses the yes / no button you could use the exit command...
cairnswm
19-05-2003, 02:03 PM
Yes there is a way to do it but dont - it is really bad coding.
As TheLion Suggests - there are better ways.
Fernando
19-05-2003, 03:51 PM
Yes!
But I have other things in mind: Look, let's say I have this code:
try
...
GetMem(p, 10000);
Object.SomeProcedure();
OtherObject.Procedure(10, 10);
open:= OpenSomething();
if open = FALSE then GoToExceptEnd;
...
except
FreeMem(p);
Object.Free;
OtherObject.Free;
CloseSomething();
end;
In this example, I have made calls to 4 procedures/functions: GetMem, SomeProcedure, Procedure and OpenSomething.
If something goes wrong with one of them, I have to free all the resources used by the code. I do that in the except/end statement.
But the problem is that the fourth function (OpenSomething) don't raise exceptions. Instead of this, it returns FALSE or TRUE. If it returns FALSE, I would like the code jump to the Except End to, as if an exception were raised.
Is there a way to do that?
Thank you!
Sander
19-05-2003, 04:38 PM
Well, if you really want to, you could try putting it either in a try..finally block(but only if you need to free those things in any case, which I doubt). OR you could put the freeing in a seperate procedure, and do it like this:
try
...
GetMem(p, 10000);
Object.SomeProcedure();
OtherObject.Procedure(10, 10);
open:= OpenSomething();
if open = FALSE then FreeProcedure;
...
except
FreeProcedure;
end;
FreeProcedure;
begin
FreeMem(p);
Object.Free;
OtherObject.Free;
CloseSomething();
end;
But there might be other ways....
Harry Hunt
19-05-2003, 07:07 PM
try
AllocateSomeMemory(X);
except
ShowAWarning;
finally
if Assigned(X) then
FreeMemory(X);
end;
Useless Hacker
19-05-2003, 08:50 PM
How about raising an exception yourself if the function returns false?
try
...
if open = FALSE then raise Exception.Create('open failed');
...
except
FreeMem(p);
Object.Free;
OtherObject.Free;
CloseSomething();
end;
Fernando
20-05-2003, 01:47 AM
My god! A lot of solutions! :-)
THank you guys! I enjoyed all the examples!
Fernando
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.