![](/img/trans.png)
[英]Error “unreported exception java.io.ioexception must be caught or declared to be thrown” in Java class
[英]Error message "unreported exception java.io.IOException; must be caught or declared to be thrown"
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();
}
}
void showfile() throws java.io.IOException <-----
您的showfile()
方法拋出IOException
,因此無論何時使用它,您都必須捕獲該異常或再次拋出它。 就像是:
try {
showfile();
}
catch(IOException e) {
e.printStackTrace();
}
您應該了解Java 中的異常。
異常在堆棧中冒泡。 如果調用者調用一個拋出已檢查異常的方法,比如 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 來顯式拋出異常(如果允許的話)。
如果該方法覆蓋在接口或超類中定義的方法簽名,而該方法簽名並未聲明該方法可能會拋出該異常,則您不能聲明它拋出異常。
錯誤消息意味着任何調用showfile()
必須要么聲明它反過來拋出IOException
,要么調用必須在捕獲IOException
的try
塊內。 當您調用showfile()
,您不會執行這些操作; 例如,您的filecontent
構造函數既不聲明IOException
也不包含try
塊。
目的是某個方法在某處應該包含一個try
塊,並捕獲並處理此異常。 編譯器試圖強迫您在某處處理異常。
順便說一句,這段代碼(抱歉這么直率)很糟糕。 你沒有關閉你打開的任何文件, BufferedReader
總是指向第一個文件,即使你似乎試圖讓它指向另一個文件,循環包含會導致各種異常的逐個錯誤,等等。當你得到這個編譯,也不會像您期望的工作。 我認為你需要放慢一點。
您的方法 showFile() 聲明它可以拋出 IOException。 由於這是一個已檢查的異常,因此對 showFile() 方法的任何調用都必須以某種方式處理異常。 一種選擇是在 try-catch 塊中包裝對 showFile() 的調用。
try {
showFile();
}
catch(IOException e) {
// Code to handle an IOException here
}
當被調用者拋出一個異常,即void showfile() 拋出 java.io.IOException 時,調用者應該處理它或再次拋出它。
並且還學習命名約定。 類名應該以大寫字母開頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.