comp.lang.ada
 help / color / mirror / Atom feed
From: David Trudgett <wpower@zeta.org.au.nospamplease>
Subject: Handling exceptions -- hiding exceptions from calling code
Date: Sun, 07 Aug 2005 17:08:12 +1000
Date: 2005-08-07T17:08:12+10:00	[thread overview]
Message-ID: <m3hde2i5r7.fsf@rr.trudgett> (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




             reply	other threads:[~2005-08-07  7:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-07  7:08 David Trudgett [this message]
2005-08-07  7:28 ` Handling exceptions -- hiding exceptions from calling code tmoran
2005-08-07  9:20   ` David Trudgett
replies disabled

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