错误消息“未报告的异常java.io.IOException;必须被抓住或宣布被抛出“ [英] Error message "unreported exception java.io.IOException; must be caught or declared to be thrown"
问题描述
filecontent.java:15:未报告的异常java.io.IOException;必须是
抓住或
宣布抛出
filecontent.java:15: unreported exception java.io.IOException; must be caught or declared to be thrown
showfile();
^ filecontent.java:78:未报告的异常java.io.IOException;必须被捕获或声明为
抛出
showfile(); ^ filecontent.java:78: unreported exception java.io.IOException; must be caught or declared to be thrown
showfile();
^
showfile(); ^
我已经抛出了java.io.IOException,但它仍然显示了这些错误。
I have already thrown java.io.IOException, but still it shows these errors.
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
,所以每当你使用它时你必须捕获该异常或再次钍。类似于:
Your showfile()
method throws IOException
, so whenever you use it you have to either catch that exception or again thorw it. Something like:
try {
showfile();
}
catch(IOException e) {
e.printStackTrace();
}
你应该了解<一个href =http://docs.oracle.com/javase/tutorial/essential/exceptions/ =noreferrer> Java中的例外。
这篇关于错误消息“未报告的异常java.io.IOException;必须被抓住或宣布被抛出“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!