[英]Java: try-catch error, must be caught to be thrown
我試圖創建一種加載文件的方法,但它沒有按應有的方式工作。 為什么我會收到這個錯誤? 我的 try-catch 塊有問題嗎?
NamnMetod.java:157: error: unreported exception InterruptedException; must be caught or declared to be thrown
EventQueue.invokeAndWait(new Runnable() {
這是我的代碼:
public static void hämtaFrånText() {
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,"Filen hittades inte!");
}
catch(IOException e2) {
JOptionPane.showMessageDialog(null,"Det misslyckades");
}
}
});
}
它與run()
方法中的 try/catch 塊無關。 問題在於調用invokeAndWait
的方法...... EventQueue.invokeAndWait()
被聲明為拋出InterruptedException
,這是一個已檢查的異常......所以你需要另一個try/catch 塊(圍繞調用)或你的hämtaFrånText
方法應該聲明它也可以拋出InterruptedException
。
根據JavaDoc (強調我自己的):
public static void invokeAndWait(Runnable runnable) 拋出InterruptedException , InvocationTargetException
invokeAndWait
可以拋出兩種類型的異常。 在您的方法中,您沒有try-catch
段來處理這些錯誤,因此您的方法必須指定它本身可能會拋出這些異常,因為它們不是在內部處理的。
您需要:
throws InterruptedException
添加到您的方法簽名或EventQueue.invokeAndWait(new Runnable() {...
的try-catch
塊,以便可以處理任何異常。定義匿名類:
new Runnable() {
@Override public void run() { ... }
};
基本上是定義本地類的簡寫:
class MyAnonymousRunnable implements Runnable {
@Override public void run() { ... }
}
然后創建該類的一個實例:
new MyAnonymousRunnable();
因此,您的代碼可以寫成:
EventQueue.invokeAndWait(new MyAnonymousRunnable());
只要你有一個合適的MyAnonymousRunnable
定義。 如果你這樣做,你會在該行得到完全相同的編譯錯誤。 但是,您知道如何在沒有匿名類的情況下捕獲代碼中的異常:
try {
EventQueue.invokeAndWait(new MyAnonymousRunnable());
} catch (InterruptedException e) {
Thread.currentThread().interrrupt();
// Do whatever to handle the exception.
}
因此,如果您匿名定義類,則沒有真正的區別:
try {
EventQueue.invokeAndWait(new Runnable() {
@Override public void run() { ... }
});
} catch (InterruptedException e) {
Thread.currentThread().interrrupt();
// Do whatever to handle the exception.
}
您可以封裝整個EventQueue.invokeAndWait(new Runnable(){...});
另一個try-catch
塊中的代碼如下:
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, "Filen hittades inte!");
} catch(IOException e2) {
JOptionPane.showMessageDialog(null, "Det misslyckades");
}
}
});
} catch(InterruptedException e3) {
// your catch code here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.