comp.lang.ada
 help / color / mirror / Atom feed
* Handling Exceptions?
@ 2003-11-04 21:13 Freejack
  2003-11-05  0:13 ` Ludovic Brenta
  0 siblings, 1 reply; 6+ messages in thread
From: Freejack @ 2003-11-04 21:13 UTC (permalink / raw)


I've recently switched back to using Adasockets for my programming needs.

I never bothered to catch exceptions before, since none of my apps were
really that important. So, to get a little practice I slung together the
following program(in about 5 minutes.) using Adasockets.

The problem is that the compiler will let me use the exceptions declared
in the first declaritive part but not the second declaritive part.

Any pointers would be appreciated.

Freejack.

with Sockets;
with Sockets.Naming;
with Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
procedure newconn is
	-- Or "Learning to use exceptions the right way." --
	package TIO renames Ada.Text_IO;
	package SockNames renames Sockets.Naming;
	NameQuery : String := "www.yahoo.com"; -- Line Filler --
	HostIP : SockNames.Address;
	HostImage : String(1..14);
	LocalHost : String := "127.0.0.1";
	LocalIP	  : String := "<edited for posting to C.L.A.>";
begin
	-- This code left over from an cut+paste operation --
	-- And it still works.							   --
	HostIP := SockNames.Address_Of(NameQuery);
	HostImage := SockNames.Image(HostIP);
	TIO.Put_Line(HostImage);
	-----------------------------------------------------
	declare
		Site: Sockets.Socket_FD;
		use Sockets;
	begin

		Socket(Site, AF_INET, SOCK_STREAM);
		Connect(Site, LocalHost, 9);	-- Discard service --
		exception
			when Connection_Refused =>
				TIO.Put_Line("Connection Refused.");
			when others =>
				TIO.Put_Line("Something else went wrong");
		Sockets.Shutdown(Site, Both);
	end;

	declare
		Echo : Sockets.Socket_FD;
		EchoString : String(1..10);
		EchoResponse : String(1..10); -- Could get less. Wont get more than 10 --
		use Sockets;
	
		-- This may look a little fruity, but I just wanna see if it'll work.  --
		package RandChar is new Ada.Numerics.Discrete_Random(Character); 
		use RandChar;
		G : Generator;

	begin
		Socket(Echo, AF_INET, SOCK_STREAM);
		Connect(Echo, LocalIP, 7);
		exception
			when Connection_Refused =>
				TIO.Put_Line("Connection Refused");
				exit;
			when others =>
				Sockets.Shutdown(Echo, Both);
				TIO.Put_Line("There was a problem connecting to Echo");
				exit;
		
		
		-- Loop a bunch of times and send a bunch of shit to Echo.	  --
		-- And get a bunch of shit back.				  --
		
		EchoString := "1234567890";	
		Reset(G);	
		
		for X in 1..EchoString'Length loop
			-- First we send the String. --
			Put(Echo, EchoString);
			exception
				when Connection_Closed =>
					TIO.Put_Line("Connection Closed Prematurely");
					TIO.Put_Line("Terminating Application.");
					Shutdown(Echo, Both);
					exit; -- This should break us out of the loop? --
			-- Then we get it back. --
			EchoResponse := Get_Line(Echo, 10);
			exception
				when Connection_Closed =>
					TIO.Put_Line("Peer Closed Connection before sending whole line");
					TIO.Put_Line("Got characters" & EchoResponse &".");
					TIO.Put_Line("Terminating Application.");
					Shutdown(Echo, Both);
					exit;
			
			-- Now we modify our string a bit(or a byte, pun intended) --
			EchoString(X) := Random(G);
		end loop;
		Shutdown(Echo, Both);
	end;
			
end newconn;



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

* Re: Handling Exceptions?
  2003-11-04 21:13 Handling Exceptions? Freejack
@ 2003-11-05  0:13 ` Ludovic Brenta
  2003-11-05  0:20   ` Ludovic Brenta
  0 siblings, 1 reply; 6+ messages in thread
From: Ludovic Brenta @ 2003-11-05  0:13 UTC (permalink / raw)


Freejack <user@nospam.net> writes:

> I've recently switched back to using Adasockets for my programming needs.
> 
> I never bothered to catch exceptions before, since none of my apps were
> really that important. So, to get a little practice I slung together the
> following program(in about 5 minutes.) using Adasockets.
> 
> The problem is that the compiler will let me use the exceptions declared
> in the first declaritive part but not the second declaritive part.
> 
> Any pointers would be appreciated.

The exception handler is the portion of your code comprised between
the "exception" keyword and the next "end;" or "end loop;" statement.
Thus, after a casual reading of your code, here are my comments:

> with Sockets;
> with Sockets.Naming;
> with Ada.Text_IO;
> with Ada.Numerics.Discrete_Random;
> procedure newconn is
> 	-- Or "Learning to use exceptions the right way." --
> 	package TIO renames Ada.Text_IO;
> 	package SockNames renames Sockets.Naming;
> 	NameQuery : String := "www.yahoo.com"; -- Line Filler --
> 	HostIP : SockNames.Address;
> 	HostImage : String(1..14);
> 	LocalHost : String := "127.0.0.1";
> 	LocalIP	  : String := "<edited for posting to C.L.A.>";
> begin
> 	-- This code left over from an cut+paste operation --
> 	-- And it still works.							   --
> 	HostIP := SockNames.Address_Of(NameQuery);
> 	HostImage := SockNames.Image(HostIP);
> 	TIO.Put_Line(HostImage);
> 	-----------------------------------------------------
> 	declare
> 		Site: Sockets.Socket_FD;
> 		use Sockets;
> 	begin
> 
> 		Socket(Site, AF_INET, SOCK_STREAM);
> 		Connect(Site, LocalHost, 9);	-- Discard service --
> 		exception
> 			when Connection_Refused =>
> 				TIO.Put_Line("Connection Refused.");
> 			when others =>
> 				TIO.Put_Line("Something else went wrong");
> 		Sockets.Shutdown(Site, Both);
> 	end;

Here ends your first exception handler.  So far, so good.  Note
however that you only call Sockets.Shutdown when handling "others",
contrary to what your indentation seems to suggest.
 
> 	declare
> 		Echo : Sockets.Socket_FD;
> 		EchoString : String(1..10);
> 		EchoResponse : String(1..10); -- Could get less. Wont get more than 10 --
> 		use Sockets;
> 	
> 		-- This may look a little fruity, but I just wanna see if it'll work.  --
> 		package RandChar is new Ada.Numerics.Discrete_Random(Character); 
> 		use RandChar;
> 		G : Generator;
> 
> 	begin
> 		Socket(Echo, AF_INET, SOCK_STREAM);
> 		Connect(Echo, LocalIP, 7);
> 		exception
> 			when Connection_Refused =>
> 				TIO.Put_Line("Connection Refused");
> 				exit;
> 			when others =>
> 				Sockets.Shutdown(Echo, Both);
> 				TIO.Put_Line("There was a problem connecting to Echo");
> 				exit;

All of the code after this point is dead code because of the "exit"
statement above, but, although your indentation seems to imply
otherwise, the exception handler for "others" really continues here.

> 		-- Loop a bunch of times and send a bunch of shit to Echo.	  --
> 		-- And get a bunch of shit back.				  --
> 		
> 		EchoString := "1234567890";	
> 		Reset(G);	
> 		
> 		for X in 1..EchoString'Length loop
> 			-- First we send the String. --
> 			Put(Echo, EchoString);
> 			exception
> 				when Connection_Closed =>
> 					TIO.Put_Line("Connection Closed Prematurely");
> 					TIO.Put_Line("Terminating Application.");
> 					Shutdown(Echo, Both);
> 					exit; -- This should break us out of the loop? --
> 			-- Then we get it back. --

Again, the "exit" statement does not end this exception handler.  You
need "end" or "end loop".  We are still within the dead code.

> 			EchoResponse := Get_Line(Echo, 10);
> 			exception
> 				when Connection_Closed =>
> 					TIO.Put_Line("Peer Closed Connection before sending whole line");
> 					TIO.Put_Line("Got characters" & EchoResponse &".");
> 					TIO.Put_Line("Terminating Application.");
> 					Shutdown(Echo, Both);
> 					exit;
> 			
> 			-- Now we modify our string a bit(or a byte, pun intended) --
> 			EchoString(X) := Random(G);
> 		end loop;
> 		Shutdown(Echo, Both);
> 	end;

Now this really ends your second exception handler.
	
> end newconn;

The RM and the Rationale recommend that you indent the "exception"
keyword at the same level as the "begin" keyword, like this:

declare
   -- declarations
begin
   -- statements
exception
   when others =>
      -- statements
end;

This better reflects the grammatical structure for exception handlers.

If you need to execute several statements with different handlers, you
need several "begin" blocks.  You did this properly for the first
exception handler, not for the ones that follow.

HTH

-- 
Ludovic Brenta.



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

* Re: Handling Exceptions?
  2003-11-05  0:13 ` Ludovic Brenta
@ 2003-11-05  0:20   ` Ludovic Brenta
  2003-11-05  6:57     ` Freejack
  0 siblings, 1 reply; 6+ messages in thread
From: Ludovic Brenta @ 2003-11-05  0:20 UTC (permalink / raw)


Ludovic Brenta <ludovic.brenta@insalien.org> writes:
> The RM and the Rationale recommend that you indent the "exception"
> keyword at the same level as the "begin" keyword, like this:
> 
> declare
>    -- declarations
> begin
>    -- statements
> exception
>    when others =>
>       -- statements
> end;
> 
> This better reflects the grammatical structure for exception handlers.


And, I might add, I personally recommend you use Emacs with ada-mode,
which indents automatically for you as you type.  I have been heard to
say "There is only one editor, but Vim is the other" :)

(and yes, I write this in Emacs using Gnus).

-- 
Ludovic Brenta.



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

* Re: Handling Exceptions?
  2003-11-05  0:20   ` Ludovic Brenta
@ 2003-11-05  6:57     ` Freejack
  2003-11-05  9:09       ` tmoran
  2003-11-05 12:33       ` David C. Hoos
  0 siblings, 2 replies; 6+ messages in thread
From: Freejack @ 2003-11-05  6:57 UTC (permalink / raw)


On Tue, 04 Nov 2003 19:20:48 -0500, Ludovic Brenta wrote:

> And, I might add, I personally recommend you use Emacs with ada-mode,
> which indents automatically for you as you type.  I have been heard to
> say "There is only one editor, but Vim is the other" :)
> 
> (and yes, I write this in Emacs using Gnus).
 
Thanks.

It seems I was more than a little fuzzy on exceptions.

It appears that the end of an exception handler is denoted by the "end"
keyword. Sort of the way a military drill instructor denotes stopping by
the word "Halt!".
Hence the phrases  "end;", "end loop;" , "end if;", and "end
<procedure | function>;" all would also enclose an exception handler. An
exception handler has scope throughout the entire declared section unless
propogated.

Is my understanding correct here?

Thanks for the clarification.

Freejack.



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

* Re: Handling Exceptions?
  2003-11-05  6:57     ` Freejack
@ 2003-11-05  9:09       ` tmoran
  2003-11-05 12:33       ` David C. Hoos
  1 sibling, 0 replies; 6+ messages in thread
From: tmoran @ 2003-11-05  9:09 UTC (permalink / raw)


> Hence the phrases  "end;", "end loop;" , "end if;", and "end
> <procedure | function>;" all would also enclose an exception handler.
  No.
Don't think of exception handlers as just some other kind of statements,
that you can drop in just ahead of the word "end".  Think instead of a
"handled sequence of statements" - a set of statements to which the
exception handler(s) apply, the word "exception", and the handlers.

The end of the "handled sequence of statements" is indeed signalled by the
keyword "end", but there is also a beginning to the "handled sequence of
statements" and it's a "begin" (or "accept ...  do").



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

* Re: Handling Exceptions?
  2003-11-05  6:57     ` Freejack
  2003-11-05  9:09       ` tmoran
@ 2003-11-05 12:33       ` David C. Hoos
  1 sibling, 0 replies; 6+ messages in thread
From: David C. Hoos @ 2003-11-05 12:33 UTC (permalink / raw)


Read the Ada95 Reference Manual, sections 11.4 through 11.4.2 for a
discussion of exception handling with example.

"Freejack" <user@nospam.net> wrote in message
news:pan.2003.11.05.07.03.50.808072.762@nospam.net...
> On Tue, 04 Nov 2003 19:20:48 -0500, Ludovic Brenta wrote:
>
> > And, I might add, I personally recommend you use Emacs with ada-mode,
> > which indents automatically for you as you type.  I have been heard to
> > say "There is only one editor, but Vim is the other" :)
> >
> > (and yes, I write this in Emacs using Gnus).
>
> Thanks.
>
> It seems I was more than a little fuzzy on exceptions.
>
> It appears that the end of an exception handler is denoted by the "end"
> keyword. Sort of the way a military drill instructor denotes stopping by
> the word "Halt!".
> Hence the phrases  "end;", "end loop;" , "end if;", and "end
> <procedure | function>;" all would also enclose an exception handler. An
> exception handler has scope throughout the entire declared section unless
> propogated.
>
> Is my understanding correct here?
>
> Thanks for the clarification.
>
> Freejack.
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada-france.org
> http://www.ada-france.org/mailman/listinfo/comp.lang.ada
>
>




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

end of thread, other threads:[~2003-11-05 12:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-04 21:13 Handling Exceptions? Freejack
2003-11-05  0:13 ` Ludovic Brenta
2003-11-05  0:20   ` Ludovic Brenta
2003-11-05  6:57     ` Freejack
2003-11-05  9:09       ` tmoran
2003-11-05 12:33       ` David C. Hoos

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