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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ce37dd019a007e9a X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-12-10 14:24:13 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cpk-news-hub1.bbnplanet.com!crtntx1-snh1.gtei.net!cambridge1-snf1.gtei.net!news.gtei.net!bos-service1.ext.raytheon.com!dfw-service2.ext.raytheon.com.POSTED!not-for-mail Message-ID: <3C153609.1ED3935@Raytheon.com> From: Mark Johnson X-Mailer: Mozilla 4.5 [en] (WinNT; I) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Incompatible type error handling References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Mon, 10 Dec 2001 16:24:09 -0600 NNTP-Posting-Host: 192.27.48.44 X-Complaints-To: news@ext.ray.com X-Trace: dfw-service2.ext.raytheon.com 1008023052 192.27.48.44 (Mon, 10 Dec 2001 16:24:12 CST) NNTP-Posting-Date: Mon, 10 Dec 2001 16:24:12 CST Organization: Raytheon Company Xref: archiver1.google.com comp.lang.ada:17720 Date: 2001-12-10T16:24:09-06:00 List-Id: Ben wrote: > > Hi, > > I am new to Ada and the instruction book I found is not really helpful > in explaining how to handle errors of unmatching types. For example, > one of the sample problems the book has me do it a simple calculator > made to work with real values. Say I programmed it to work only with > integers, yet I enter a real number. That will cause a problem, > right? Hmm. It depends on what you do and how you do it. Let's try a few examples... - Integer_IO.Get - skips leading white space, reads a + or - sign, then basically reads digits. If a decimal point is provided, it stops reading at the decimal point. Exception Data_Error generated if the value is illegal (various causes). Your code at this point will likely fail on reading the rest of the line of text (finding a period instead of an operator). - Integer'Value - similar sequence, but raises Constraint_Error in case of error. What really happens depends on the code you read the string with. - read a real & assign to an integer You get a compile error (if conversion omitted) or the results of the explicit real to integer conversion (round). - read one character at a time & interpret. It would do whatever you coded it to do. As with anything I have to deal with on a computer - I suggest using the correct data types for all cases.... > Is the error handling automatic in Ada, or do I need to code > something in. So far I have been unsuccessful at finding example code > and instructions on how to do this. Not quite sure what you mean by "automatic". If you expect the run time to catch an exception and ask you to type the value in again - sorry - you have to do that yourself. Something like a begin / end with exception handler like.... begin (read the stuff here) exception when E : others => Put_Line("Can't understand input " && [their input here]); Put_Line(Exception_Message(E)); end; within a loop that keeps asking until you get the input correct. Of course, for a homework assignment, you might be tempted to just assume valid input. That will make your code about 1/3rd to 1/2 the size a robust version would be. --Mark