comp.lang.ada
 help / color / mirror / Atom feed
* Newbie Q : Exception handling
@ 2001-11-27 10:04 Stella
  2001-11-28  3:13 ` DuckE
  2001-11-28 16:15 ` Jeffrey Carter
  0 siblings, 2 replies; 7+ messages in thread
From: Stella @ 2001-11-27 10:04 UTC (permalink / raw)


Hi All,

Having a strange problem using exceptions. It's ( I presume) a pretty
basic question.

Here's the code:-

-- This program does nothing, and is just an example to indicate 
-- the exception handling problem...

with Text_Io, Ada.Integer_Text_Io;
use Text_Io, Ada.Integer_Text_Io;

procedure Exception_Handling2 is
    
    type Units is ( Ins, Ft, Yd, Mil, Mm, Cm, M, Km, Oz, Lbs, G, Kg );
    Num, Deno, Quot : Units;

    package Units_Io is new Enumeration_Io( Units );
    use Units_Io;

begin
    loop
        begin
            Put("Enter num  >"); Get( Num );
            Put("Enter quot >"); Get( Deno );
            
            Quot :=Num;
             exit;
                        
        exception
            when Data_Error => Put_Line("Wrong_d!"); 
            when Constraint_Error => Put_Line("Wrong_c");
            when others => Put_Line("Wrong_o");
        end;
        
        New_Line;
    end loop;
    
    Put("The result is "); Put( Quot,1 ); New_Line;

end Exception_Handling2;

What happens is that if I enter a garbage value ( i.e. hello ), it
traps the data_error problem fine, and asks for the data again, but if
I enter 0 or any number, it traps the error, but just goes round in an
infinite loop, i.e.
	Enter num > 	Wrong_d!

	Enter num > 	Wrong_d!

	Enter num > 	Wrong_d!

	Enter num > 	Wrong_d!

	etc etc etc

i.e. it doesn't stop at the get(), but just seems to skip over that
line. I've tried flush() to maybe get round any buffer problems, but
that does nothing.

I've found it seems to occur when numbers are input when strings  are
expected and vice-versa.

Any pointers would be gratefully recieved...

( I've tried different compilers, but that makes no difference )

Stella



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Newbie Q : Exception handling
  2001-11-27 10:04 Newbie Q : Exception handling Stella
@ 2001-11-28  3:13 ` DuckE
  2001-11-28  3:31   ` Stella
  2001-11-28 16:15 ` Jeffrey Carter
  1 sibling, 1 reply; 7+ messages in thread
From: DuckE @ 2001-11-28  3:13 UTC (permalink / raw)


Try adding a Skip_Line to your exception handler.

SteveD

"Stella" <bt_are_shite@talk21.com> wrote in message
news:3c035fe0.7469961@news.freeserve.net...
> Hi All,
>
> Having a strange problem using exceptions. It's ( I presume) a pretty
> basic question.
>
> Here's the code:-
>
> -- This program does nothing, and is just an example to indicate
> -- the exception handling problem...
>
> with Text_Io, Ada.Integer_Text_Io;
> use Text_Io, Ada.Integer_Text_Io;
>
> procedure Exception_Handling2 is
>
>     type Units is ( Ins, Ft, Yd, Mil, Mm, Cm, M, Km, Oz, Lbs, G, Kg );
>     Num, Deno, Quot : Units;
>
>     package Units_Io is new Enumeration_Io( Units );
>     use Units_Io;
>
> begin
>     loop
>         begin
>             Put("Enter num  >"); Get( Num );
>             Put("Enter quot >"); Get( Deno );
>
>             Quot :=Num;
>              exit;
>
>         exception
>             when Data_Error => Put_Line("Wrong_d!");
>             when Constraint_Error => Put_Line("Wrong_c");
>             when others => Put_Line("Wrong_o");
>         end;
>
>         New_Line;
>     end loop;
>
>     Put("The result is "); Put( Quot,1 ); New_Line;
>
> end Exception_Handling2;
>
> What happens is that if I enter a garbage value ( i.e. hello ), it
> traps the data_error problem fine, and asks for the data again, but if
> I enter 0 or any number, it traps the error, but just goes round in an
> infinite loop, i.e.
> Enter num > Wrong_d!
>
> Enter num > Wrong_d!
>
> Enter num > Wrong_d!
>
> Enter num > Wrong_d!
>
> etc etc etc
>
> i.e. it doesn't stop at the get(), but just seems to skip over that
> line. I've tried flush() to maybe get round any buffer problems, but
> that does nothing.
>
> I've found it seems to occur when numbers are input when strings  are
> expected and vice-versa.
>
> Any pointers would be gratefully recieved...
>
> ( I've tried different compilers, but that makes no difference )
>
> Stella





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Newbie Q : Exception handling
  2001-11-28  3:13 ` DuckE
@ 2001-11-28  3:31   ` Stella
  0 siblings, 0 replies; 7+ messages in thread
From: Stella @ 2001-11-28  3:31 UTC (permalink / raw)


On Wed, 28 Nov 2001 03:13:27 GMT, "DuckE" <nospam_steved94@home.com>
wrote:

>Try adding a Skip_Line to your exception handler.
>
>SteveD

That's the one. Thanks. 

Stella

>
>"Stella" <bt_are_shite@talk21.com> wrote in message
>news:3c035fe0.7469961@news.freeserve.net...
>> Hi All,
>>
>> Having a strange problem using exceptions. It's ( I presume) a pretty
>> basic question.
>>
<snip>
>> Stella
>
>




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Newbie Q : Exception handling
@ 2001-11-28  8:36 Gautier Write-only-address
  0 siblings, 0 replies; 7+ messages in thread
From: Gautier Write-only-address @ 2001-11-28  8:36 UTC (permalink / raw)
  To: comp.lang.ada

>             Put("Enter num  >"); Get( Num );
>             Put("Enter quot >"); Get( Deno );

Add a "Skip_Line;" after the Get's.

>( I've tried different compilers, but that makes no difference )
A good sign, BTW...
________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: Do not answer to sender address, visit the Web site!


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Newbie Q : Exception handling
  2001-11-27 10:04 Newbie Q : Exception handling Stella
  2001-11-28  3:13 ` DuckE
@ 2001-11-28 16:15 ` Jeffrey Carter
  2001-11-30 18:39   ` Matthew Heaney
  1 sibling, 1 reply; 7+ messages in thread
From: Jeffrey Carter @ 2001-11-28 16:15 UTC (permalink / raw)


If you read carefully the description of what Enumeration_IO.Get does,
you'll see that it looks at the digit but doesn't remove it from the
buffer, so the following Get encounters it again, resulting in an
infinite loop.

Others have suggested adding Skip_Line. This will work, but I generally
advise using Get_Line to input an entire line, then decode it Get, when
the input comes from a human.

-- 
Jeffrey Carter



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Newbie Q : Exception handling
  2001-11-28 16:15 ` Jeffrey Carter
@ 2001-11-30 18:39   ` Matthew Heaney
  2001-12-12 23:40     ` Stella
  0 siblings, 1 reply; 7+ messages in thread
From: Matthew Heaney @ 2001-11-30 18:39 UTC (permalink / raw)



"Jeffrey Carter" <jeffrey.carter@boeing.com> wrote in message
news:3C050DB2.4C327C3D@boeing.com...
> Others have suggested adding Skip_Line. This will work, but I generally
> advise using Get_Line to input an entire line, then decode it Get, when
> the input comes from a human.

I agree with this advice completely.  It's too easy to forget to call
Skip_Line when there's a problem with the input, resulting in an infinite
loop.

As Jeff suggested, use Get_Line to read in the raw text, and then get the
enum value by using the version of Get that reads from a string.

This technique also allows you to pass meta-values as input, such as "first"
or "last" or "exit", etc.






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Newbie Q : Exception handling
  2001-11-30 18:39   ` Matthew Heaney
@ 2001-12-12 23:40     ` Stella
  0 siblings, 0 replies; 7+ messages in thread
From: Stella @ 2001-12-12 23:40 UTC (permalink / raw)


On Fri, 30 Nov 2001 13:39:10 -0500, "Matthew Heaney" <mheaney@on2.com>
wrote:

>
>"Jeffrey Carter" <jeffrey.carter@boeing.com> wrote in message
>news:3C050DB2.4C327C3D@boeing.com...
>> Others have suggested adding Skip_Line. This will work, but I generally
>> advise using Get_Line to input an entire line, then decode it Get, when
>> the input comes from a human.
>
>I agree with this advice completely.  It's too easy to forget to call
>Skip_Line when there's a problem with the input, resulting in an infinite
>loop.
>
>As Jeff suggested, use Get_Line to read in the raw text, and then get the
>enum value by using the version of Get that reads from a string.
>
>This technique also allows you to pass meta-values as input, such as "first"
>or "last" or "exit", etc.
>
>
>

Thanks, much nicer...

Stella



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2001-12-12 23:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-27 10:04 Newbie Q : Exception handling Stella
2001-11-28  3:13 ` DuckE
2001-11-28  3:31   ` Stella
2001-11-28 16:15 ` Jeffrey Carter
2001-11-30 18:39   ` Matthew Heaney
2001-12-12 23:40     ` Stella
  -- strict thread matches above, loose matches on Subject: below --
2001-11-28  8:36 Gautier Write-only-address

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