【问题标题】:Error message "unreported exception java.io.IOException; must be caught or declared to be thrown"错误消息“未报告的异常 java.io.IOException;必须被捕获或声明为抛出”
【发布时间】:2012-01-03 04:25:43
【问题描述】:

错误:

filecontent.java:15: 未报告的异常 java.io.IOException;一定是 抓到或 宣布被抛出

显示文件(); ^ filecontent.java:78: 未报告的异常 java.io.IOException;必须被抓获或被宣布为 扔了

显示文件(); ^

我已经抛出了 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();
    }
}

【问题讨论】:

    标签: java ioexception throws


    【解决方案1】:
    void showfile() throws java.io.IOException  <-----
    

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

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

    你应该了解exceptions in Java

    【讨论】:

    • 你说它是 java.io.IOException 的事实拯救了我的一天。我正在导入 java.realm
    【解决方案2】:

    异常在堆栈中冒泡。如果调用者调用一个抛出检查异常的方法,比如 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 来显式抛出异常(如果允许的话)。

    如果该方法重写了在接口或超类中定义的方法签名,而该接口或超类也未声明该方法可能会引发该异常,则您不能将其声明为引发异常。

    【讨论】:

    • 我在try catch块中调用了showfile()。问题解决了。但是现在当我运行它时,它显示filenotfoundexception。
    • @AkashPatel :这可能是因为您在文件阅读器中传递了错误的文件路径。
    • 这是我创建 FileReader 对象的代码。 for(int i=0;i
    【解决方案3】:

    错误消息意味着任何调用showfile() 的方法都必须声明 依次抛出IOException,或者调用必须在捕获@ 的try 块内987654324@。当你打电话给showfile() 时,你什么都不做;例如,您的 filecontent 构造函数既不声明 IOException 也不包含 try 块。

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

    顺便说一句,这段代码是(抱歉这么直率)可怕。您不会关闭您打开的任何文件,BufferedReader 始终指向第一个文件,即使您似乎试图使其指向另一个文件,循环包含会导致各种错误异常等。当您进行编译时,它不会按您的预期工作。我认为你需要放慢速度。

    【讨论】:

    • ^^ 这个。我真的怀疑这是货物崇拜编程的教科书示例。
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多