comp.lang.ada
 help / color / mirror / Atom feed
* Handling exceptions -- hiding exceptions from calling code
@ 2005-08-07  7:08 David Trudgett
  2005-08-07  7:28 ` tmoran
  0 siblings, 1 reply; 3+ messages in thread
From: David Trudgett @ 2005-08-07  7:08 UTC (permalink / raw)



I've been doing a bit of playing around with Ada lately to try to
learn the ropes, and in the process stumbled upon a little difficulty
which hopefully has a simple answer, though my Ada book doesn't seem
to have it!

What I would like to do is write a user input routine (text console
based) that loops until a valid answer is supplied. The problem is
that exceptions can be generated (DATA_ERROR and END_ERROR in
particular) which I would like to catch in order to display a message
and then continue processing the input loop. The best I've been able
to come up with so far is the following idea, which is not robust
because of the scope for infinite recursion:

   function Read_A_Legal_Move (The_Board : in Board) return Position is
      Move : Integer := 0;
   begin

      while (Move not in Position'Range) or else
        (not Is_Valid_Move(The_Board, Move)) loop

         Put("Enter your move (1-9): ");
         Get(Move); Skip_Line;

         if Move not in Position'Range then
            Put(Move);
            Put(" is not in the range 1..9"); New_Line;
            Put("Please try again."); New_Line;
         elsif not Is_Valid_Move(The_Board, Move) then
            Put("That place is already occupied."); New_Line;
            Put("Please try again."); New_Line;
         end if;

      end loop;

      return Move;

    exception
       when Data_Error =>
          Put("That wasn't a number! Getting desperate, eh?");
          New_Line;
          Put("Please enter a whole number between one and nine.");
          New_Line;
          Skip_Line;
          return Read_A_Legal_Move(The_Board);
       when End_Error =>
          Put("Uh, uh, uh! Can't chicken out that easily!");
          New_Line;
          return Read_A_Legal_Move(The_Board);

   end Read_A_Legal_Move;


What would be the idiomatic way to accomplish this in Ada without the
possibility of infinite recursion?

Thanks,

David


-- 

David Trudgett
http://www.zeta.org.au/~wpower/

Our government has kept us in a perpetual state of fear - kept us in a
continuous stampede of patriotic fervor - with the cry of grave
national emergency....  Always there has been some terrible evil to
gobble us up if we did not blindly rally behind it by furnishing the
exorbitant sums demanded.  Yet, in retrospect, these disasters seem
never to have happened, seem never to have been quite real.

    -- Douglas MacArthur (1880-1964), 1957




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Handling exceptions -- hiding exceptions from calling code
  2005-08-07  7:08 Handling exceptions -- hiding exceptions from calling code David Trudgett
@ 2005-08-07  7:28 ` tmoran
  2005-08-07  9:20   ` David Trudgett
  0 siblings, 1 reply; 3+ messages in thread
From: tmoran @ 2005-08-07  7:28 UTC (permalink / raw)


 loop
   begin
     Get(Move);
     exit;
   exception
     when ...
       put("...");
       skip_line; -- toss any other garbage on that bad line
   end;
 end loop;
 skip_line;  -- on to the next thing

>because of the scope for infinite recursion:
   only if your user has infinite stubborness.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Handling exceptions -- hiding exceptions from calling code
  2005-08-07  7:28 ` tmoran
@ 2005-08-07  9:20   ` David Trudgett
  0 siblings, 0 replies; 3+ messages in thread
From: David Trudgett @ 2005-08-07  9:20 UTC (permalink / raw)


tmoran@acm.org writes:

>  loop
>    begin
>      Get(Move);
>      exit;
>    exception
>      when ...
>        put("...");
>        skip_line; -- toss any other garbage on that bad line
>    end;
>  end loop;
>  skip_line;  -- on to the next thing
>
>>because of the scope for infinite recursion:
>    only if your user has infinite stubborness.

Yes, indeed! ;-)

Thanks very much for that. It worked nicely! Adding an enclosing block
is obviously the key here.

David

-- 

David Trudgett
http://www.zeta.org.au/~wpower/

No one is given a map to their dreams
All we can do is to trace it.
See where we go to, know where we've been
Build up the courage to face it.

    -- Sandy Denny
    



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-08-07  9:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-07  7:08 Handling exceptions -- hiding exceptions from calling code David Trudgett
2005-08-07  7:28 ` tmoran
2005-08-07  9:20   ` David Trudgett

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