java - 错误消息 "unreported exception java.io.IOException; must be caught or declared to be thrown"

标签 java ioexception throws

错误:

filecontent.java:15: unreported exception java.io.IOException; must be caught or declared to be thrown

showfile(); ^ filecontent.java:78: unreported exception java.io.IOException; must be caught or declared to be thrown

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();
    }
}

最佳答案

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

您的 showfile() 方法会抛出 IOException,因此无论何时使用它都必须捕获该异常或再次抛出它。比如:

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

您应该了解exceptions in Java .

关于java - 错误消息 "unreported exception java.io.IOException; must be caught or declared to be thrown",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8707906/

相关文章:

android - 任务 ':app:transformClassesAndResourcesWithProguardForRelease'的执行失败:java.io.IOException-Proguard问题

java - 如果方法抛出未在方法声明中指定的异常, "throws"会发生什么

javascript - 了解 QUnit 异常测试

java - PL/SQL 中的嵌套类(对象类型)

Java 访问桥 - 使用 getAccessibleContextAt 时出现问题

java - EntityUtils .consume 返回 IOException

java - 处理异常的 throws 语句——Java

Java HashSet 包含预期值以外的值,而不是预定义值

java - 有可用的 Java 代理服务器库吗?

java - CentOS 6.5 + JasperReports 5.6.0 = 读取字体数据时出现异常