comp.lang.ada
 help / color / mirror / Atom feed
From: RLS@psu.edu    (Robert L. Spooner, AD3K)
Subject: Re: Help with Exceptions!
Date: 1996/05/09
Date: 1996-05-09T00:00:00+00:00	[thread overview]
Message-ID: <4mt0um$dgu@hearst.cac.psu.edu> (raw)
In-Reply-To: 4mqio5$a8b@news.sanders.lockheed.com


In <4mqio5$a8b@news.sanders.lockheed.com>, John Cupak <jcupak@isd99.sanders.lockheed.com> writes:
>Well, I just *have* to put in my $0.02 worth....
>
>In some cases, you *do* want to re-execute the same statement. For example,
>if you query the user for a number, and you get something else: 
>
>function Response (Prompt : in String) return Integer is
>   Line : String(1..80);
>   Size : Natural;
>begin
>   Try_Again: loop
>      Error_Handler: begin
>         Put(Prompt);                         -- Ask user for input
>         Get_Line(Line,Size);                 -- Get user input
>         return Integer'Value(Line(1..Size)); -- Raise exception if bad input
>      exception
>          when others =>
>             Put_Line("""" & Line(1..Size) & """ is not a valid value.");
>             -- Stay in Try_Again loop
>      end Error_Handler;
>   end loop Try_Again;
>end Response;
>
>I trust my Ada83 code is correct, as I didn't compile it for this response.
>
>Hope this helps!

Another way to reexecute a statement is to have the function call itself from the 
exception handler, after outputting an error message.  Once the user enters valid 
input, the function willrecursively return with the proper value.  For user input 
from a keyboard, any performance differences between the loop and the recursive function 
call will be negligible.

function Response (Prompt : String) return integer is

   line : ...
   Size : ...

begin
   Put (Prompt);
   Get_Line (...;
   return ...
exception
   when others =>
      Put_Line (...;  -- error message
      return Response(Prompt);
end Response;

>-- 
>John J. Cupak, Jr., CCP     Internet       : jcupak@isd99.sanders.lockheed.com
>Lockheed Sanders, Inc.      Lockheed LAN   : jcupak@mailgw.sanders.lockheed.com
>95 Canal Street/MER15-2403  Lockheed DECNet: NHQVAX::CUPAK 
>Nashua, NH 03061-0868       Work Telephone : 603 885-2142 FAX: 603 885-1480
>

Bob

Robert L. Spooner             Applied Research Laboratory
(814) 863-4120                             PO Box 30
RLS19@PSUVM.PSU.EDU      State College, PA 16804-0030





  parent reply	other threads:[~1996-05-09  0:00 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-05-07  0:00 Help with Exceptions! Robert Gelb
1996-05-07  0:00 ` Vincent Smeets
1996-05-07  0:00 ` Steve Howard
1996-05-07  0:00   ` Robert Dewar
     [not found]   ` <4mqio5$a8b@news.sanders.lockheed.com>
1996-05-09  0:00     ` Robert L. Spooner, AD3K [this message]
1996-05-10  0:00   ` Jon S Anthony
1996-05-10  0:00     ` Robert A Duff
1996-05-07  0:00 ` John Herro
     [not found] ` <318F94D9.35AB@io.com>
1996-05-10  0:00   ` George F.Rice
1996-05-13  0:00     ` Dave Jones
1996-05-13  0:00   ` Robert I. Eachus
1996-05-14  0:00     ` John Herro
1996-05-14  0:00       ` Robert I. Eachus
1996-05-14  0:00   ` Theodore E. Dennison
1996-05-14  0:00     ` Robert A Duff
1996-05-14  0:00 ` Michel Gauthier
1996-05-14  0:00   ` Robert A Duff
1996-05-15  0:00     ` Norman H. Cohen
1996-05-15  0:00       ` Robert A Duff
1996-05-15  0:00 ` Michel Gauthier
1996-05-16  0:00 ` Jon S Anthony
1996-05-16  0:00 ` Jon S Anthony
1996-05-16  0:00   ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
1996-05-09  0:00 tmoran
replies disabled

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