[英]Java BufferedReader error with try block
我有這個應該加載文件的方法,但它給了我這個錯誤:
NamnMetod.java:175: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
BufferedReader inFil = new BufferedReader(new FileReader(fil));
^
NamnMetod.java:177: error: unreported exception IOException; must be caught or declared to be thrown
String rad = inFil.readLine();
^
這是我的代碼:
public static void hämtaFrånText() {
try {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
String aktuellMapp = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(aktuellMapp);
int resultat = fc.showOpenDialog(null);
if (resultat != JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, "Ingen fil valdes!");
System.exit(0);
}
String fil = fc.getSelectedFile().getAbsolutePath();
String[] namn = new String[3];
String output ="";
BufferedReader inFil = new BufferedReader(new FileReader(fil));
String rad = inFil.readLine();
int antal = 0;
while(rad != null){
namn[antal] = rad;
rad = inFil.readLine();
antal++;
}
inFil.close();
}
});
}catch(IOException e2) {
JOptionPane.showMessageDialog(null,"The file was not found!");
}
}
我很困惑,因為我同時捕獲了 IOException 和 FileNotFoundException 但我仍然收到此錯誤...
您在創建Runnable
的代碼中捕獲它們,而需要在Runnable.run()
捕獲異常。
在run
方法中移動 try/catch。
此外,使用 try-with-resources 確保 FileReader 始終關閉,即使發生異常:
try (FileReader fr = new FileReader(fil);
BufferedReader inFil = new BufferedReader(fr)) {
// ...
// No need to close `fr` or `inFil` explicitly.
}
下面是解決方案:
public static void hämtaFrånText() {
try {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
String aktuellMapp = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(aktuellMapp);
int resultat = fc.showOpenDialog(null);
if (resultat != JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, "Ingen fil valdes!");
System.exit(0);
}
String fil = fc.getSelectedFile().getAbsolutePath();
String[] namn = new String[3];
String output = "";
BufferedReader inFil = new BufferedReader(new FileReader(fil));
String rad = inFil.readLine();
int antal = 0;
while (rad != null) {
namn[antal] = rad;
rad = inFil.readLine();
antal++;
}
inFil.close();
}catch(FileNotFoundException e1) {
JOptionPane.showMessageDialog(null,"The file was not found!");
}
catch(IOException e2) {
JOptionPane.showMessageDialog(null, "Failed!");
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Reason
:run 是 Runnable 中的一個方法,並且在 eventQueue 中它已經定義,並且在方法定義中沒有處理異常或在方法聲明語句中添加 throw 的地方。 因此 FileNoFoundException 和 IOException 都應該在方法內部而不是外部給出。
此外,EventQueue 拋出了 InterruptedException 檢查異常,我添加了另一個 try catch 來處理該異常。
希望能幫助到你。
你這里有范圍問題。
您正在使用的 try 和 catch 將捕獲hämtaFrånText()
的異常,但不會捕獲run()
方法的異常
異常可以在方法內部發生run
不出來的一面。
您提供的 try-Cathc 將只關心在其范圍內發生的異常,因為您沒有拋出這些異常,而不是您的hämtaFrånText()
方法來處理run()
內部發生的事情。
如果run
是您定義的方法,那么您可以拋出它們。 但現在你唯一的選擇是在run()
方法中捕獲它們。
所以試試
public void run() {
try{
.............
...........
}catch(FileNotFoundException e1) {
JOptionPane.showMessageDialog(null,"The file was not found!");
}
catch(IOException e2) {
JOptionPane.showMessageDialog(null, "Failed!");
}
}
try-catch 塊屬於“Runnable”的 run()-Method - 這個 run()-Method 不能拋出任何異常。 提交 Runnable 不會拋出異常。
您需要在 run 方法中移動 try catch 塊,這應該可以解決問題,並且您還需要處理EventQueue.invokeAndWait(Runnable)
拋出的InvocationTargetException
和InterruptedException
以正確編譯您的類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.