Java: try-catch 错误,必须被捕获才能抛出
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)
throws InterruptedException,
InvocationTargetException
invokeAndWait
可以抛出两种类型的异常。在您的方法中,您没有 try-catch
段来处理这些错误,因此您的方法必须指定它可能自己抛出这些异常,因为它们没有在内部处理。
您需要:
- 将
throws InterruptedException
添加到您的方法签名或
- 有一个包含
EventQueue.invokeAndWait(new Runnable() {...
的 try-catch
块,以便可以处理任何异常。
正在定义匿名 class:
new Runnable() {
@Override public void run() { ... }
};
基本上是 shorthand 用于定义本地 class:
class MyAnonymousRunnable implements Runnable {
@Override public void run() { ... }
}
然后创建一个 class:
的实例
new MyAnonymousRunnable();
因此,您的代码可以写成:
EventQueue.invokeAndWait(new MyAnonymousRunnable());
前提是您对 MyAnonymousRunnable
有一个合适的定义。
如果你这样做,你会在该行得到完全相同的编译错误。但是,您知道如何在没有匿名 class:
的情况下捕获代码中的异常
try {
EventQueue.invokeAndWait(new MyAnonymousRunnable());
} catch (InterruptedException e) {
Thread.currentThread().interrrupt();
// Do whatever to handle the exception.
}
因此,如果您匿名定义 class,则没有真正的区别:
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
}
}
我试图创建一种加载文件的方法,但它没有按预期方式工作。为什么会出现此错误?我的 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) throws InterruptedException, InvocationTargetException
invokeAndWait
可以抛出两种类型的异常。在您的方法中,您没有 try-catch
段来处理这些错误,因此您的方法必须指定它可能自己抛出这些异常,因为它们没有在内部处理。
您需要:
- 将
throws InterruptedException
添加到您的方法签名或 - 有一个包含
EventQueue.invokeAndWait(new Runnable() {...
的try-catch
块,以便可以处理任何异常。
正在定义匿名 class:
new Runnable() {
@Override public void run() { ... }
};
基本上是 shorthand 用于定义本地 class:
class MyAnonymousRunnable implements Runnable {
@Override public void run() { ... }
}
然后创建一个 class:
的实例new MyAnonymousRunnable();
因此,您的代码可以写成:
EventQueue.invokeAndWait(new MyAnonymousRunnable());
前提是您对 MyAnonymousRunnable
有一个合适的定义。
如果你这样做,你会在该行得到完全相同的编译错误。但是,您知道如何在没有匿名 class:
try {
EventQueue.invokeAndWait(new MyAnonymousRunnable());
} catch (InterruptedException e) {
Thread.currentThread().interrrupt();
// Do whatever to handle the exception.
}
因此,如果您匿名定义 class,则没有真正的区别:
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
}
}