在java中如何让一个类既实现MouseListener又抛出IOException?

发布于 2024-12-18 00:53:58 字数 33 浏览 8 评论 0原文

抛出 IOException 是针对来自文件的输入。

The throws IOException is for input from a file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷爱 2024-12-25 00:53:58

像这样的东西:

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) {}
}

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) {}
}
兲鉂ぱ嘚淚 2024-12-25 00:53:58

类不会引发异常,方法会引发异常。

public class Foo implements MouseListener {
    public void throwingUp() throws IOException {
        // ... Code that could throw IOE
    }
    // ... MouseListener impl
}

Classes don't throw exceptions, methods do.

public class Foo implements MouseListener {
    public void throwingUp() throws IOException {
        // ... Code that could throw IOE
    }
    // ... MouseListener impl
}
懒的傷心 2024-12-25 00:53:58

类不会抛出 IOException,而方法会。您的类可以实现 MouseListener,但这些方法(mouseClicked、mousePressed 等)不能抛出 IOException。您必须将它们包装在 RuntimeException (或子类)中。例如,

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

其他方法,例如 methodThatMightThrowAnIOException(),可以抛出 IOException。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文