首页 文章

错误消息“未报告的异常java.io.IOException;必须 grab 或宣布被抛出“

提问于
浏览
20

错误:

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 回答

  • 0

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

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

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

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

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

    你的 showfile() 方法会抛出 IOException ,所以每当你使用它时,你必须捕获该异常或再次使用它 . 就像是:

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

    你应该了解exceptions in Java .

  • 6

    错误消息表示任何调用 showfile() 的方法必须声明它反过来抛出 IOException ,或者调用必须位于捕获 IOExceptiontry 块内 . 当你致电 showfile() 时,你不会做这些;例如,您的 filecontent 构造函数既不声明 IOException 也不包含 try 块 .

    目的是某个方法在某个地方应该包含 try 块,并捕获并处理此异常 . 编译器试图强迫您在某处处理异常 .

    顺便说一下,这段代码(对不起,是如此生硬)可怕 . 你没有关闭你打开的任何文件, BufferedReader 总是指向第一个文件,即使你似乎试图让它指向另一个文件,循环包含将导致各种异常的一个一个错误,当你编译它时,它将无法正常工作 . 我想你需要放慢一点 .

  • 1

    异常在堆栈中冒泡 . 如果调用者调用抛出已检查异常的方法(如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()方法正在抛出,那么无论代码调用该方法必须捕获什么代码,或者通过在方法签名中添加相同的抛出IOException(如果允许的话)来显式抛出异常 .

    如果该方法覆盖在接口或超类中定义的方法签名,该方法签名也不声明该方法可能抛出该异常,则不能声明它抛出异常 .

相关问题