错误消息“未报告的exceptionjava.io.IOException; 必须被抓或宣布被抛出“

错误:

filecontent.java:15:未报告的exceptionjava.io.IOException; 必须被抓住或宣布被抛出

演出文件(); ^ filecontent.java:78:未报告的exceptionjava.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(); } } 

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

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

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

您应该了解Java中的exception 。

错误消息意味着任何调用showfile()必须声明反过来抛出IOException ,或者调用必须在捕获IOExceptiontry块内。 当你调用showfile() ,你不会这些; 例如,您的filecontent构造函数既不声明IOException也不包含try块。

目的是某个方法在某个地方应该包含一个try块,并捕获并处理这个exception。 编译器试图强制您在某处处理exception

顺便说一句,这段代码(很抱歉是如此生硬) 可怕 。 你没有关闭你打开的任何文件, BufferedReader总是指向第一个文件,即使你似乎试图让它指向另一个文件,循环包含将导致各种exception的一个一个错误,如果你要编译它,它将无法按预期工作。 我想你需要放慢一点。

exception在堆栈中冒泡。 如果调用者调用抛出已检查exception的方法(如IOException),它也必须捕获exception,或者自己抛出exception。

在第一个块的情况下:

 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,因为它的签名由接口决定,因此您必须在以下内容中捕获exception:

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

请记住,showFile()方法抛出exception; 这就是“throws”关键字表示该方法可能抛出该exception的内容。 如果showFile()方法正在抛出,那么无论代码调用该方法必须捕获什么代码,或者它们自己通过在方法签名中添加相同的抛出IOException(如果允许的话)来显式抛出exception。

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

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

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

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

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