comp.lang.ada
 help / color / mirror / Atom feed
From: Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject: Re: I am not understanding user defined exceptions
Date: Fri, 03 Feb 2017 20:26:09 -0500
Date: 2017-02-03T20:26:09-05:00	[thread overview]
Message-ID: <k3ba9chta4hqdcv4q6npmrdbffsnfeoi8l@4ax.com> (raw)
In-Reply-To: 6cb6e781-c6df-4962-99e3-760e7c7fab88@googlegroups.com

On Fri, 3 Feb 2017 12:27:09 -0800 (PST), patrick@spellingbeewinnars.org
declaimed the following:

>
>If we type something like :    
>         if x = 1
>         then
>           raise Foo_Error;
>         end if ;
> 
>will x not be tested for exceptions conditions automatically from that point onward? If so, why would it not raise an exception if the second text_io.get came back with 1 ?
>

	No... because that is just a sequence of statements that only take
effect /at that location/. Ada doesn't magically go back and apply that
code whenever you do something with the variable X.

	If you always need to have input of a 1 raise the exception, you need
to encapsulate the input with the test. See below:

-=-=-=-=-=-
with Ada.Integer_Text_Io;
with Ada.Text_IO;

procedure Test_Exceptions is
   
   Foo_Error : exception;
   
   X : Integer;
   
   procedure Get_Int (An_Int : out Integer) is
   begin
      Ada.Text_IO.Put ("Enter an integer => ");
      
      Ada.Integer_Text_IO.Get (An_Int);
      
      if An_Int = 1 then
	 raise Foo_Error;
      end if;
   end Get_Int;
   
begin
   Get_Int (X);   -- first input point
   Get_Int (X);    -- second input point
   
   Ada.Text_Io.Put_Line ("Ended without exceptions...");
      
exception
   when Foo_Error =>
      Ada.Text_Io.Put_Line ("Error handled...");
      
end Test_Exceptions;

-=-=-=-=-=-

C:\Users\Wulfraed\Ada>test_exceptions
Enter an integer => 1
Error handled...

C:\Users\Wulfraed\Ada>test_exceptions
Enter an integer => 2
Enter an integer => 1
Error handled...

C:\Users\Wulfraed\Ada>test_exceptions
Enter an integer => 3
Enter an integer => 2
Ended without exceptions...
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/


  parent reply	other threads:[~2017-02-04  1:26 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-03 20:27 I am not understanding user defined exceptions patrick
2017-02-03 21:08 ` Randy Brukardt
2017-02-03 22:41   ` patrick
2017-02-04  1:26 ` Dennis Lee Bieber [this message]
2017-02-04  6:58   ` J-P. Rosen
2017-02-04 17:08     ` Simon Wright
2017-02-08 17:55       ` Georg Bauhaus
2017-02-08 23:37         ` Randy Brukardt
2017-02-09 19:08           ` Robert A Duff
2017-02-09 21:47             ` Randy Brukardt
2017-02-09 22:52               ` Robert A Duff
2017-02-10  9:52                 ` Simon Wright
2017-02-10 10:11                   ` Dmitry A. Kazakov
2017-02-10 20:56                 ` Randy Brukardt
2017-02-10 21:09                 ` Randy Brukardt
2017-02-10 22:07                   ` Dmitry A. Kazakov
2017-02-13 23:20                     ` Randy Brukardt
2017-02-14  8:39                       ` Dmitry A. Kazakov
2017-02-14 20:07                         ` Randy Brukardt
2017-02-15  9:32                           ` Dmitry A. Kazakov
2017-02-10 23:53                   ` Shark8
2017-02-09  0:36         ` Robert A Duff
2017-02-09  7:43           ` Simon Wright
2017-02-09 19:15             ` Robert A Duff
2017-02-09 20:39               ` Randy Brukardt
2017-02-09 21:06               ` Randy Brukardt
2017-02-09 23:08                 ` Robert A Duff
2017-02-04  8:41 ` Simon Wright
replies disabled

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