Re: RFC: About error handling - WAS: Re: A remobjects wtf...



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.
.



Relevant Pages

  • Re: Running a parameter query based on multi select list box values
    ... provided I had previously used to build my report data upon. ... MsgBox "You must select a value from the Agency list", vbOKOnly, ... 'Trim the strAgency variable and add the "IN clause used in the query ... For Each x In .ItemsSelected ' Loop through the values ...
    (microsoft.public.access.modulesdaovba)
  • ACC2000: bloat after saving report via code
    ... So I have a routine that makes a PDF for each customer in a loop. ... customer, save the report, and makes a PDF from the changed report. ... This error is from the Docmd above when I open the report in design mode ...
    (microsoft.public.access.reports)
  • Re: RFC: About error handling - WAS: Re: A remobjects wtf...
    ... Of course in this example (and probably in most cases, as there is some condition that determines whether you need to retry) it could be replaced with a test for a valid BaudRate in 'until'. ... You could be against repeat and while in general with the same argument. ... I would also say that your 'Retry' examples resembles these much more (You wouldn't need 'goto Succeeded', would you?): ... 'break') applies to this 'fake' loop, ...
    (borland.public.delphi.non-technical)
  • Re: Maximising report previews
    ... This means pausing the Do loop in order to take the necessary ... One way of doing this is to open the report in dialogue mode and a second is ... to open it from an intermediate form which is itself opened in dialogue mode. ... form which was itself opened in dialogue mode then, even though the maximise ...
    (microsoft.public.access.formscoding)
  • Re: lazy evaluation?
    ... repeat do ... What confused me about this is that retry has two meanings, ... context of a loop or block, and another in the context of begin...end ...
    (comp.lang.ruby)