From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,803d147296719896 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!x35g2000prf.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Puzzling small piece of code - exception block essentially used as goto Date: Mon, 04 Jun 2007 17:08:17 -0700 Organization: http://groups.google.com Message-ID: <1181002097.027416.324060@x35g2000prf.googlegroups.com> References: NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1181002097 11682 127.0.0.1 (5 Jun 2007 00:08:17 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 5 Jun 2007 00:08:17 +0000 (UTC) In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: x35g2000prf.googlegroups.com; posting-host=66.126.103.122; posting-account=cw1zeQwAAABOY2vF_g6V_9cdsyY_wV9w Xref: g2news1.google.com comp.lang.ada:16065 Date: 2007-06-04T17:08:17-07:00 List-Id: On Jun 1, 10:01 pm, Anonymous Coward wrote: > This is an excerpt from some code I recently encountered (it may give > some of you a headache): > > Still_Looping := True; > > Delta_Beyond_Max_Minutes: > while Still_Looping loop > > Handle_Max_Minute_Error: > begin > > Hour_Delta := New_Hour - Hour; > Still_Looping := False; > Hour := New_Hour; > > exception > when Constraint_Error => > > if New_Hour > Hour then > > Hour_Delta := Max_Hour_Delta > > else > > Hour_Delta := Relative_Time - Max_Hour_Delta; > > end if; > > Hour := Hour + Hour_Delta; > > end Handle_Max_Minute_Error; > end loop Delta_Beyond_Max_Minutes; > > Tests show that this code actually loops a few times when run in a > typical state. What I would like clarification on is when the > exception block ends, where is control returned to? It goes to the point after the block it's part of, i.e. Handle_Max_Minute_Error. At that point, it hits the "end loop" and loops back, if Still_Looping is still true. It looks like the code is expecting that a Constraint_Error might be raised on Hour_Delta := New_Hour - Hour; Since I don't see the declarations of any of those variables, I don't know whether it's expecting a Constraint_Error on the subtraction operation, or on the assignment of the final result to Hour_Delta (if Hour_Delta is a subrange). But the purpose of the code seems to be that: if the above subtraction succeeds, we're done; if not, it adjusts some values and tries again. > Also, anyone up for the challenge of rewriting this without the > exception handler so the rest of us humans can understand what it's > doing? As noted above, this is really impossible without knowing how those three key variables are declared. However, it's possible to write something more readable even with an exception handler. Here's how I might do it: Delta_Beyond_Max_Minutes: loop begin Hour_Delta := New_Hour - Hour; Subtraction_Succeeds := True; exception when Constraint_Error => Subtraction_Succeeds := False; end; if Subtraction_Succeeds then Hour := New_Hour; exit; else if New_Hour > Hour then Hour_Delta := Max_Hour_Delta else Hour_Delta := Relative_Time - Max_Hour_Delta; end if; Hour := Hour + Hour_Delta; end if; end loop Delta_Beyond_Max_Minutes; Is this easier to understand? (I'm assuming that "Hour := New_Hour;" won't raise an exception.) -- Adam