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,e18b90266d1f7532,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news3.google.com!news.glorb.com!newscon02.news.prodigy.com!newscon06.news.prodigy.com!prodigy.net!newsfeed.pacific.net.au!nasal.pacific.net.au!not-for-mail Newsgroups: comp.lang.ada Subject: Handling exceptions -- hiding exceptions from calling code From: David Trudgett Organization: Very little? Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:dRcTGtmqyC9F8Xe+ONxqi9z/heA= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Sun, 07 Aug 2005 17:08:12 +1000 NNTP-Posting-Host: 61.8.41.3 X-Complaints-To: news@pacific.net.au X-Trace: nasal.pacific.net.au 1123398750 61.8.41.3 (Sun, 07 Aug 2005 17:12:30 EST) NNTP-Posting-Date: Sun, 07 Aug 2005 17:12:30 EST Xref: g2news1.google.com comp.lang.ada:4011 Date: 2005-08-07T17:08:12+10:00 List-Id: 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