comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Puzzling small piece of code - exception block essentially used as goto
Date: Mon, 04 Jun 2007 17:08:17 -0700
Date: 2007-06-04T17:08:17-07:00	[thread overview]
Message-ID: <1181002097.027416.324060@x35g2000prf.googlegroups.com> (raw)
In-Reply-To: <vQ68i.99$UD4.49@trndny07>

On Jun 1, 10:01 pm, Anonymous Coward <anonym...@coward.org> 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






  parent reply	other threads:[~2007-06-05  0:08 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-02  5:01 Puzzling small piece of code - exception block essentially used as goto Anonymous Coward
2007-06-02  5:48 ` Jeffrey R. Carter
2007-06-05  2:38   ` Anonymous Coward
2007-06-05  4:10     ` tmoran
2007-06-05  4:53       ` tmoran
2007-06-02  6:16 ` tmoran
2007-06-05  0:08 ` Adam Beneschan [this message]
2007-06-05  7:58 ` anon
2007-06-05 10:40   ` Markus E Leypold
2007-06-05 23:43   ` Anonymous Coward
2007-06-06 10:39     ` Markus E Leypold
2007-06-08  0:55       ` Anonymous Coward
2007-06-08  2:22         ` Markus E Leypold
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox