Tim
Tim

Reputation: 21

Java unreported exception confusing me

Hey guys I've been working on some buttons for my GUI, and I decided to implement some previous code.

However, I'm getting an error when I try to compile. In line 141 in my code (specifically, the last button) I am told that I have an unreported IOException that must be caught or declared to be thrown.

My code is below:

public void actionPerformed(ActionEvent ae) {
    if ((ae.getSource() == button5) && (!connected)) {
        try {
            s = new Socket("127.0.0.1", 2020);
            pw = new PrintWriter(s.getOutputStream(), true);
        } catch (UnknownHostException uhe) {
            System.out.println(uhe.getMessage());
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
        connected = true;
        t = new Thread(this);
        //b.setEnabled(false);
        button5.setLabel("Disconnect");
        t.start();
    } else if ((ae.getSource() == button5) && (connected)) {
        connected = false;
        try {
            s.close(); //no buffering so, ok
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
        //System.exit(0);
        button5.setLabel("Connect");
    } else {
        temp = tf.getText();
        pw.println(temp);
        tf.setText("");
    }
    if (ae.getActionCommand().equals("Save it")) {
        Scanner scan = new Scanner(System.in);
        try {
            PrintWriter pw = new PrintWriter(new FileWriter(new File("test.txt")));
            for (;;) {
                String temp = scan.nextLine();
                if (temp.equals("")) {
                    break;
                }
                pw.println(temp);
            }
            pw.close();
        } catch (IOException ioe) {
            System.out.println("IO Exception! " + ioe.getMessage());
        }
    } else if (ae.getActionCommand().equals("Load it")) {
        Scanner scan = new Scanner(System.in);
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File("test.txt")));
            String temp = "";
            while ((temp = br.readLine()) != null) {
                System.out.println(temp);
            }
            br.close();
        } catch (FileNotFoundException fnfe) {
            System.out.println("Input file not found.");
        } catch (IOException ioe) {
            System.out.println("IO Exception! " + ioe.getMessage());
        }
    } else if (ae.getActionCommand().equals("Clear it")) {
        ta.setText("");
    } else {
        PrintWriter pw = new PrintWriter(new FileWriter(new File("test.txt")));

    }
}

Upvotes: 1

Views: 176

Answers (2)

GreySage
GreySage

Reputation: 1180

In general, any IO operation can potentially cause an exception. Depending on what you want, the easiest solution is just put throws IOException at the top of the method where you see the problem, but this isn't very good practice, and doesn't work in this case. Putting a try/catch block around the problem line, and including a meaningful error message, is probably the best way to go.

Upvotes: 0

user3314809
user3314809

Reputation: 80

just add a try/catch block to the following code (end of what you posted):

else{
PrintWriter pw = new PrintWriter (
            new FileWriter(
            new File("test.txt")));

}}

like so:

else{
        try{
            PrintWriter pw = new PrintWriter (new FileWriter(new File("test.txt")));
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
}}

Upvotes: 1

Related Questions