错误信息 "未报告的异常 java.io.IOException;必须被捕获或声明抛出"

38

错误:

filecontent.java:15: 未报告的异常 java.io.IOException;必须被捕获或声明为抛出

showfile(); ^ filecontent.java:78: 未报告的异常 java.io.IOException;必须被捕获或声明为抛出

showfile(); ^

我已经抛出了 java.io.IOException,但仍然显示这些错误。

我的代码:

import java.awt.*;
import java.awt.event.*;
import java.io.*;

class filecontent extends Frame implements ActionListener
{
    TextField t[] = new TextField[4];
    TextArea ta[] = new TextArea[4];
    Button submit;
    Panel p1;
    filecontent()
    {
        setGUI();
        setRegister();
        showfile();
        setTitle("FileData");
        setVisible(true);
        setSize(300, 300);
        /*  addWindowListener(new WindowAdapter()
            { 
                public void windowClosing(WindowEvent we)
                { 
                    System.exit(0); 
                }
            }); 
        */

    }

    void setGUI()
    {
        p1 = new Panel();
        p1.setLayout(new GridLayout(5, 4, 10, 10));
        for(int i=0; i<4; i++)
        {
            t[i] = new TextField(10);
            ta[i] = new TextArea("");
            p1.add(t[i]);
            p1.add(ta[i]);
        }
        submit = new Button("Submit");
        p1.add(submit);
    }

    void setRegister()
    {
        submit.addActionListener(this);
    }

    void showfile() throws java.io.IOException
    {
        FileReader fin[] = new FileReader[4];
        FileReader fn;
        //   br[]=new BufferedReader[4];

        for(int i=0;i<4;i++)
        {
            fin[i]=new FileReader(t[i].getText());
        }
        int cnt = 1;
        String s;
        fn = fin[0];
        BufferedReader br = new BufferedReader(fn);
        while(cnt <= 4)
        {
            if((s=br.readLine()) != null)
            {
                ta[cnt-1].append(s+"");
            }
            else
            {
                cnt++;
                fn = fin[cnt-1];
                ta[cnt-1].setText("");
            }
        }
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==submit)
        {
            showfile();
        }
    }

    public static void main(String ar[])
    {
        new filecontent();
    }
}
5个回答

64
void showfile() throws java.io.IOException  <-----

你的showfile()方法抛出了一个IOException异常,所以每当你使用它时,你必须要么捕获这个异常,要么重新抛出它。可以像这样:

try {
  showfile();
}
catch(IOException e) {
  e.printStackTrace();
}

你应该学习有关Java中的异常


你说这是 java.io.IOException 真的帮了我大忙。我之前一直在导入 java.realm。 - Renan Vasconcelos

11

异常会一级级往上传递。如果一个调用者调用了一个会抛出已检查异常(比如 IOException)的方法,那么它必须要么捕获该异常,要么自己抛出该异常。

对于第一个代码块:

filecontent()
{
    setGUI();
    setRegister();
    showfile();
    setTitle("FileData");
    setVisible(true);
    setSize(300, 300);

    /*
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    */
}

你需要包含一个try catch块:

filecontent()
{
    setGUI();
    setRegister();
    try {
        showfile();
    }
    catch (IOException e) {
        // Do something here
    }
    setTitle("FileData");
    setVisible(true);
    setSize(300, 300);

    /*
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    */
}
在第二种情况下:
public void actionPerformed(ActionEvent ae)
{
    if (ae.getSource() == submit)
    {
        showfile();
    }
}

由于该方法的签名是由接口确定的,因此您无法从该方法中抛出IOException异常,因此必须在其中捕获异常:

public void actionPerformed(ActionEvent ae)
{
    if(ae.getSource()==submit)
    {
        try {
            showfile();
        }
        catch (IOException e) {
            // Do something here
        }
    }
}

记住,showFile()方法抛出了异常;这就是"throws"关键字所表示的方法可能会抛出该异常。如果showFile()方法正在抛出异常,那么调用该方法的任何代码都必须捕获异常,或者在方法签名中包含相同的throws IOException条目明确地抛出异常(如果允许)。

如果该方法重写了一个在接口或超类中定义的方法签名,并且该接口或超类没有声明该方法可能会抛出该异常,那么您不能声明它会抛出异常。


我将showfile()的调用放在try catch块中,问题已经解决。但现在当我运行它时,它显示filenotfoundexception。 - Akash Patel
@AkashPatel:这可能是因为您在文件读取器中传递了错误的文件路径。 - Harry Joy
这里是我创建FileReader对象的代码。for(int i=0;i<4;i++) { fin[i]=new FileReader(t[i].getText()); }其中,t[i]是textfield数组t[]中的一个元素。 - Akash Patel

8
这个错误消息的意思是调用 showfile() 方法的任何方法必须声明它反过来会抛出 IOException,或者该调用必须位于捕获 IOExceptiontry 块内。当你调用 showfile() 时,你都没有做到这些;例如,你的 filecontent 构造函数既没有声明 IOException,也没有包含 try 块。

意图是某个方法,在某个地方,应该包含一个 try 块,并捕获和处理此异常。编译器试图强制你在某个地方处理异常。

顺便说一句,这段代码(很抱歉要这么直接)非常糟糕。你没有关闭任何打开的文件,BufferedReader 总是指向第一个文件,即使你似乎想让它指向另一个文件,循环包含会导致各种异常的 off-by-one 错误等等。当你成功编译后,它将不会按照你的预期工作。我认为你需要放慢速度。


1
我非常怀疑这是典型的模仿编程。 - Brian Roach

3

你的showFile()方法声明可以抛出IOException异常。由于这是一个已检查的异常,因此任何调用showFile()方法的地方必须以某种方式处理异常。其中一种选择是在try-catch块中包装对showFile()的调用。

 try {
     showFile();
 }
 catch(IOException e) {
    // Code to handle an IOException here
 }

0

当被调用者抛出异常时,即 void showfile() throws java.io.IOException ,调用者应该处理它或再次抛出它。

还要学习命名约定。类名应以大写字母开头。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接