[4813] in java-interest

home help back first fref pref prev next nref lref last post

Scoping rules of try blocks????????????????

daemon@ATHENA.MIT.EDU (Walter Szewelanczyk)
Mon Jan 15 20:32:19 1996

From: "Walter Szewelanczyk" <Walter@stumpy.adco.com>
To: java-interest@java.sun.com
Date: Mon, 15 Jan 1996 19:22:19 +0000

Hi all,

I have questions as to the scoping rules of try blocks.

Last night  I was trying to implement a very simple finger/whois 
server.  This is my first java program so I thought I would make it 
simple.  I is just a subclass of Panel that takes in a host name,
a user name and when you hit the query button it starts a thread that 
actually goes out and gets the data and shows it in a textarea box.

My problem is when I first tried it I put all of my code in one try 
block.  Everything worked fine.  I later wanted to add a fwe more 
detailed error messages to my output so I used two try blocks.  The 
first just had the MySocket = new Socket(Host,Port); line in it 
followed by a couple of catch statements  and the rest of the code 
was in another try block.  I now get a error stating :

WS.java:227: Variable MySocket may not have been initialized.
                        psOut = new PrintStream(MySocket.getOutputStream());
                                                ^
1 error

Do I have to put all the code in one try block   if so why?????
Is the scoping of the try block only allowing MySocket to be valid in 
that block even though it is declard above ther try block????

Any help will be greatly appreciated.   :-)

following are the two implimentations of my NetQueryThread  (sorry if 
the tabs are weird)



This one works fine:

	public NetQueryThread(int port, String host, String user, TextArea output)
	{
		super();

		Host = host;
		User = user;
		Output = output;
		Port = port;
	}

	
	public void run()
	{
		Socket MySocket;
		PrintStream psOut;
		InputStream isIn;

		Output.setText("Connecting......");
		
		try
		{
			MySocket = new Socket(Host,Port);
			Output.setText("Connected to Host : "+Host);
			psOut = new PrintStream(MySocket.getOutputStream());
			isIn = MySocket.getInputStream();

			Output.setText("Sending Command(s)....");
			psOut.println(User);
			Output.setText("Waiting for response");
	 	
			int Bytes;
			int BytesTotal = 0;
			int BytesLeft  = 10240;
			byte Buf[] = new byte[BytesLeft];
	
			while( (BytesLeft!=0) && ((Bytes = isIn.read(Buf,BytesTotal,BytesLeft))!= -1) )
			{
				BytesTotal 	+= Bytes;
				BytesLeft		-= Bytes;
			}
		
			String str = new String(Buf,0,0,BytesTotal);
			Output.setText(str);
			MySocket.close();		
			
		}
		catch(UnknownHostException E)
		{
			Output.setText("ERROR : "+Host+" is an invalid address");
			stop();
		}
		catch(IOException E)
		{
			Output.setText("ERROR : NETWORK IO...........Closing connection");
		}
		
		stop();		
	}
}





This one won't doesn't :

class NetQueryThread extends Thread
{
	int				Port;
	String 		Host;
	String 		User;
	TextArea 	Output;

	public NetQueryThread(int port, String host, String user, TextArea output)
	{
		super();

		Host = host;
		User = user;
		Output = output;
		Port = port;
	}

	
	public void run()
	{
		Socket MySocket;
		PrintStream psOut;
		InputStream isIn;

		Output.setText("Connecting......");

		try
		{
			MySocket = new Socket(Host,Port);
		}
		catch(UnknownHostException E)
		{
			Output.setText("ERROR : "+Host+" is an invalid address");
			stop();
		}
		catch(IOException E)
		{
			Output.setText("ERROR : unable to connect to server on "+Host);
			stop();
		}
		
		Output.setText("Connected to Host : "+Host);
		
		try
		{
/* >>>>>>>>>>>>>> */	psOut = new PrintStream(MySocket.getOutputStream());
			isIn = MySocket.getInputStream();

			Output.setText("Sending Command(s)....");
			psOut.println(User);
			Output.setText("Waiting for response");
	 	
			int Bytes;
			int BytesTotal = 0;
			int BytesLeft  = 10240;
			byte Buf[] = new byte[BytesLeft];
	
			while( (BytesLeft!=0) && ((Bytes = isIn.read(Buf,BytesTotal,BytesLeft))!= -1) )
			{
				BytesTotal 	+= Bytes;
				BytesLeft		-= Bytes;
			}
		
			String str = new String(Buf,0,0,BytesTotal);
			Output.setText(str);
			MySocket.close();		
			
		}
		catch(IOException E)
		{
			Output.setText("ERROR : NETWORK IO...........Closing connection");
		}
		
		stop();		
	}

}




Thanks in advance,
Walt

********************************************************************************************
Walter Szewelanczyk					Technical Director 
Walter@adco.com						NET VENTURE, Inc.


	"One must stay in a very RIGID state of FLEXABILITY"

********************************************************************************************
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post