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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC,WEIRD_QUOTING autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,deeb88b0e7eede4f X-Google-Attributes: gid103376,public From: RLS@psu.edu (Robert L. Spooner, AD3K) Subject: Re: Help with Exceptions! Date: 1996/05/09 Message-ID: <4mt0um$dgu@hearst.cac.psu.edu>#1/1 X-Deja-AN: 153895473 references: <4mmimq$s4r@hatathli.csulb.edu> <318F353D.4531@mtm.syr.ge.com> <4mqio5$a8b@news.sanders.lockheed.com> organization: Applied Research Laboratory, The Pennsylvania State Universitiy reply-to: RLS19@psuvm.psu.edu newsgroups: comp.lang.ada Date: 1996-05-09T00:00:00+00:00 List-Id: In <4mqio5$a8b@news.sanders.lockheed.com>, John Cupak 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