Aka*_*tel 25 java ioexception throws
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Har*_*Joy 48
void showfile() throws java.io.IOException <-----
Run Code Online (Sandbox Code Playgroud)
你的showfile()方法抛出IOException,所以每当你使用它时,你必须捕获该异常或再次使用它.就像是:
try {
showfile();
}
catch(IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
您应该了解Java中的异常.
错误消息意味着任何调用方法showfile()必须声明它反过来抛出IOException,或者调用必须在try捕获的块内IOException.当你打电话时showfile(),你不会这些; 例如,您的filecontent构造函数既不声明IOException也不包含try块.
目的是某个方法在某个地方应该包含一个try块,并捕获并处理这个异常.编译器试图强迫您在某处处理异常.
顺便说一下,这段代码(对不起,是如此生硬)可怕.你没有关闭你打开的任何文件,BufferedReader总是指向第一个文件,即使你似乎试图使它指向另一个文件,循环包含将导致各种异常的一个一个错误,等等当你确实编译它时,它将无法正常工作.我想你需要放慢一点.
异常在堆栈中冒泡.如果调用者调用抛出已检查异常的方法(如IOException),它也必须捕获异常,或者自己抛出异常.
在第一个块的情况下:
filecontent()
{
setGUI();
setRegister();
showfile();
setTitle("FileData");
setVisible(true);
setSize(300, 300);
/*
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
*/
}
Run Code Online (Sandbox Code Playgroud)
你必须包含一个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);
}
});
*/
}
Run Code Online (Sandbox Code Playgroud)
在第二种情况下:
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == submit)
{
showfile();
}
}
Run Code Online (Sandbox Code Playgroud)
您不能从此方法中抛出IOException,因为它的签名由接口决定,因此您必须在以下内容中捕获异常:
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==submit)
{
try {
showfile();
}
catch (IOException e) {
// Do something here
}
}
}
Run Code Online (Sandbox Code Playgroud)
请记住,showFile()方法抛出异常; 这就是"throws"关键字表示该方法可能抛出该异常的原因.如果showFile()方法正在抛出,那么无论代码调用该方法必须捕获什么代码,或者通过在方法签名中添加相同的抛出IOException(如果允许)来显式抛出异常.
如果该方法覆盖在接口或超类中定义的方法签名,该方法签名也不声明该方法可能抛出该异常,则不能声明它抛出异常.
| 归档时间: |
|
| 查看次数: |
128892 次 |
| 最近记录: |