aspose file tools*
The moose likes Beginning Java and the fly likes random Try error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "random Try error" Watch "random Try error" New topic
Author

random Try error

Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
Ok, here is the code that I am using (it's an inner class):

class sendButtonListener implements ActionListener {

public void actionPerformed(ActionEvent ev) {

try {

writer.println(userInput.getText());
writer.flush();

} catch (IOException ex) {

ex.printStackTrace();

}

userInput.setText("");
userInput.requestFocus();

} // end of action method

} // end of inner class

and the only compile error I am getting is:

exception java.io.IOException is never thrown in body of corresponding try statement
} catch (IOException ex) {
^

Any body have any ideas? I mean it's a try followed by a catch, I really don't see the problem.

Thanks ahead of time.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
If the exception is never going to be thrown, why bother trying to catch it.
I can understand it making sense to have a warning instead of an error, but there may be a reason for this.


Joanne
Dave Wood
bronco
Ranch Hand

Joined: Aug 02, 2004
Posts: 161
You may need to post a little more code...at first glance, this looks reasonable. But what type is "writer" and where is it defined?


Co-Author of <a href="http://www.oreilly.com/catalog/jswing2" target="_blank" rel="nofollow">Java Swing</a><br />Co-Creator of <a href="http://www.sun.com/training/catalog/courses/CX-310-055.xml" target="_blank" rel="nofollow">SCJP 5.0</a> and <a href="http://www.sun.com/training/certification/java/associate_beta.xml" target="_blank" rel="nofollow">SCJA</a> exams
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
I just wasn't sure if you guys wanted to wade through it all, but here you go (I haven't coded any receiving info yet).

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.net.*;
import java.io.*;

public class Chat {

Socket chatSocket;
JTextArea chatArea;
JTextField userInput;
JTextField userID;
BufferedReader reader;
PrintWriter writer;

public static void main(String[] args) {

Chat myChat = new Chat();
myChat.go();

}

public void go() {

JFrame frame = new JFrame("Test Chat Program");
JPanel chatPanel = new JPanel();
chatArea = new JTextArea(10,20);
chatArea.setLineWrap(true);
chatArea.setEditable(false);
chatArea.setWrapStyleWord(true);
userID = new JTextField(10);
JLabel nameLabel = new JLabel("userName:");
userInput = new JTextField(20);
JScrollPane scroller = new JScrollPane(chatArea);

scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
chatPanel.add(scroller);
chatPanel.add(nameLabel);
chatPanel.add(userID);
chatPanel.add(userInput);
JButton sendButton = new JButton("SEND");
sendButton.addActionListener(new sendButtonListener());
createNetwork();

// adding the stuff to the fram, and making it visibile, etc.
frame.getContentPane().add(BorderLayout.CENTER, chatPanel);
frame.getContentPane().add(BorderLayout.SOUTH, sendButton);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);

}

// connecting to a chat server

private void createNetwork() {

// need a try since it could obviously fail

try {

// creating the actual connection

chatSocket = new Socket("192.168.0.1.", 6999);
InputStreamReader stream = new InputStreamReader(chatSocket.getInputStream());
reader = new BufferedReader(stream);
writer = new PrintWriter(chatSocket.getOutputStream());
System.out.println("networking established");

} catch (IOException ex) {

ex.printStackTrace();

}

} // end of setUpNetworking Method

// send the information to the server

class sendButtonListener implements ActionListener {

public void actionPerformed(ActionEvent ev) {

try {

writer.println(userInput.getText());
writer.flush();

} catch (IOException ex) {

ex.printStackTrace();

}

// clear the input field

userInput.setText("");
userInput.requestFocus();

} // end of action method

} // end of inner class

} // end of class
[ September 01, 2005: Message edited by: Nicholas Carrier ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

As the error message is telling you, neither PwinrWriter.println() nor PrintWriter.flush() is declared to throw IOException. You can safely remove the try block.


[Jess in Action][AskingGoodQuestions]
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
Ah, thank you, and I should have realized this because as the doc says "Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError()." I hate when the answer was kind of infront of me and I just couldn't see it.

On a side note: Would I really want to remove the try block, because I wrapped it in an Exception (instead of an IOException) and it worked fine? Basically my question is, wouldn't I want to wrap it in some sort of an exception, because if I didn't, and the connection broke (cat chewed the wire or something retarded like that) and I tried to send something, and didn't have a try block around the PrintWriter methods, wouldn't that crash the program or something horrible like that? Then again I'm still noobalicious and could be mistaken

Thanks again.
[ September 01, 2005: Message edited by: Nicholas Carrier ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If the connection closes, you won't get an exception of any kind. The methods will simply return without doing anything. You can insert calls to checkError() as the Javadoc says, and deal with the error if you choose. But no, the try block really isn't necessary.
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
cool, thanks
 
Don't get me started about those stupid light bulbs.
 
subject: random Try error