comp.lang.ada
 help / color / mirror / Atom feed
From: Mark Johnson <Mark_H_Johnson@Raytheon.com>
Subject: Re: Incompatible type error handling
Date: Mon, 10 Dec 2001 16:24:09 -0600
Date: 2001-12-10T16:24:09-06:00	[thread overview]
Message-ID: <3C153609.1ED3935@Raytheon.com> (raw)
In-Reply-To: bf8dbe70.0112101352.20a3d257@posting.google.com

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



  reply	other threads:[~2001-12-10 22:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-10 21:52 Incompatible type error handling Ben
2001-12-10 22:24 ` Mark Johnson [this message]
2001-12-10 23:03 ` Mark Lundquist
2001-12-11  3:19 ` Steve Doiel
replies disabled

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