2

The throws IOException is for input from a file.

Zane
  • 159
  • 2
  • 5

3 Answers3

4

something like this:

import java.awt.event.*;
import java.io.*;
class Foo implements MouseListener {
    void io() throws IOException {
        File file = new File("foo");
        FileReader fileReader = new FileReader(file);
        fileReader.read();
        // ...
    }
    @Override public void mouseClicked(MouseEvent arg0) {
        try {
            io();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Override public void mouseEntered(MouseEvent arg0) {}
    @Override public void mouseExited(MouseEvent arg0) {}
    @Override public void mousePressed(MouseEvent arg0) {}
    @Override public void mouseReleased(MouseEvent arg0) {}
}
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • 2
    Good answer. But I would argue that if you're needing to do any IO, it's probably best not to do it in the EventQueue thread as it may cause hold ups (causing the GUI to become unresponsive). Creating a new Runnable for a new Thread or Executor would be a better idea. – Dunes Nov 24 '11 at 00:40
2

Classes don't throw exceptions, methods do.

public class Foo implements MouseListener {
    public void throwingUp() throws IOException {
        // ... Code that could throw IOE
    }
    // ... MouseListener impl
}
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • You could throw a RuntimeException in a static initializer. But since Zane wants to throw an IOException, that won't do. – emory Nov 24 '11 at 00:02
  • @emory Nor would you have to ask how, either, since RTEs aren't declared. – Dave Newton Nov 24 '11 at 00:02
  • I thought they did, but when I try to have the method throws IOException it says "mousePressed(java.awt.event.MouseEvent) in TicTacToe.ClickListener cannot implement mousePressed(java.awt.event.MouseEvent) in java.awt.event.MouseListener; overridden method does not throw java.io.IOException" what does this mean? I feel silly right now – Zane Nov 24 '11 at 00:07
  • @Zane See Ray's answer; he read your mind better than I did. It's important to be specific when asking questions. Nutshell: the `MouseListener` methods can't throw it, but they can wrap a method throwing an IOE and throw a `RuntimeException` (if that's reasonable in your app). – Dave Newton Nov 24 '11 at 00:09
  • @Dave yeah, I understand now and got my program working, thank you! – Zane Nov 24 '11 at 00:34
0

Classes do not throw IOExceptions, methods do. Your class can implement MouseListener, but those methods (mouseClicked, mousePressed etc...) cannot throw IOExceptions. You'll have to wrap them in a RuntimeException (or a subclass). e.g.

   @Override
   public void mouseEntered(MouseEvent e) {
      try {
         methodThatMightThrowAnIOException();
      }
      catch (IOException ioe)
      {
         throw new RuntimeException(ioe);
      }
   }

Other methods, like methodThatMightThrowAnIOException(), can throw IOExceptions.

user949300
  • 15,364
  • 7
  • 35
  • 66