Re: RFC: About error handling - WAS: Re: A remobjects wtf...
- From: "m. Th." <a@xxxxx>
- Date: Mon, 07 Jul 2008 11:22:43 +0300
m. Th. wrote:
2. How do you see a 'retry' clause in a try/except block? Eg.
Hi guys,
I just put a QC report as a (non-final ;-)) conclusion of our discussion. Many thanks to everyone. If I missed someone on the 'credits' part, let me know :-) . Also, if someone thinks that something isn't in right place please drop a line. For the ones who are interested to vote (if there are any) here it goes:
Report No: 64173 Status: Reported
Retry keyword in 'try' blocks.
Reported by: m. Th.
Reported date: 07 Jun 2008 01:18
http://qc.codegear.com/wc/qcmain.aspx?d=64173
[Description]
Hi,
Please implement the 'Retry' keyword. This will allow jumping at the beginning of the 'try' block in a similar manner with the behavior of 'Continue' in for/while/repeat loops.
Example:
nBaudRate:=19200;
try
SendMessage('foo', nBaudRate);
except
on E: ETransmissionError do
begin
nBaudRate:=nBaudRate-100; //whatever
if nBaudRate>4800 then
Retry //goes at the beginning of 'try' block
else
raise Error.Create('Cannot send');
end;
end;
Discussion:
Even if above can be simulated by using fake loops, the main disadvantage is that the fake loop 'hides' a possible real loop and thus the developer cannot use 'Break/Continue' for the outside real loop. Example:
nBaudRate:=19200;
for i := 0 to myCollection.Count - 1 do //a real loop
begin
bSucceeded:=True;
repeat //fake loop to mimic the 'Retry' behavior
Try
SendMessage('foo', nBaudRate);
Except
On E: ETransmissionError do
Begin
bSucceeded := False; //go again
nBaudRate := nBaudRate-100;
End;
End;
Until bSucceeded Or (nBaudRate <=4800);
If Not bSucceeded Then
Raise Error.Create('Cannot send');
end;
In the above piece of code, if the developer tries to control the flow of the 'for' loop (the "real" loop) inside of try..except block, he cannot because the 'break/continue' will apply to the 'fake' loop used to mimic the 'retry' (until..repeat). Also there are other workarounds based on 'while' and recursion worth mentioning, but we stop here due of report readability reasons.
Also, as an aside the code without 'Retry' isn't so readable and needs a variable in plus (bSucceded) in order to maintain state. Again, about code quality having in code a bSucceded:=False usually forces the programmer to have a look to the code in order to see what didn't 'succeeded' and to explore the effects of this assignment, while a 'Retry' gives a clear indication that the computation will go (again) at the beginning of the most inner 'try' block.
Another point is that the management/lifetime of the status variable can be tedious when we have the above structure spanned on several entities, for example in two methods of the same class or (even worse) different classes, the 1st method implementing the try/except block and the other method (in fact this is usually an event) having the code which is executed in the 'try' part or (as in the above example) the custom exception handler.
Special tanks for helping me in getting this report goes to (in alphabetical order):
Adrian Gallero
asdad
Chris Rolliston
Jens Muhlenhoff
Jouni Aro
--
m. th.
.
- Follow-Ups:
- References:
- Re: A remobjects wtf...
- From: Farshad
- Re: A remobjects wtf...
- From: Allen Bauer (CodeGear)
- Re: A remobjects wtf...
- From: Allen Bauer (CodeGear)
- Re: A remobjects wtf...
- From: m. Th.
- RFC: About error handling - WAS: Re: A remobjects wtf...
- From: m. Th.
- Re: A remobjects wtf...
- Prev by Date: Re: Query about Delphi 2007 (W32) Pro/Enterprise versions
- Next by Date: Re: Apple, iPhone and application submissions.
- Previous by thread: Re: RFC: About error handling - WAS: Re: A remobjects wtf...
- Next by thread: Re: RFC: About error handling - WAS: Re: A remobjects wtf...
- Index(es):
Relevant Pages
|