异常的一般规则是在捕获一般异常之前先捕获更具体的异常。我有一个案例,关闭服务器抛出SocketException: socket closed
来自我的侦听器IOException
,但我不想在用户关闭服务器时向他们显示该消息。其他所有内容都可能是实际错误,因此应该向他们显示,因此我捕获SocketException
,检查其消息,如果不是socket closed
,则应将其重新抛出以被捕获并作为IOException
. Java/NetBeans 7.0.1 似乎不喜欢这样。这是我的代码:
public void run() {
while (runner == Thread.currentThread()) {
System.out.println("waiting for connection...");
try {
Socket s = server.accept(); //throws SocketException/IOException
if (session == null) {
session = new ReceiveSession(s, parent);
} else {
s.close();
}
} catch (SocketException e) {
if (!e.getMessage().equals("socket closed")) {
throw e; //error line, "unreported exception SocketException"
}
} catch (IOException e) {
e.printStackTrace();
parent.showError("Someone tried to connect but the connection failed: " + e);
session = null;
}
}
}
在尝试清理和构建时,我得到:
error: unreported exception SocketException; must be caught or declared to be thrown
throw e;
1 error
既然是SocketException
extends IOException
,就应该被抓的比较普遍IOException
。为什么我会收到此错误?(顺便说一句,在 NetBeans 中按原样运行项目可以完美运行,当服务器按照我的意愿关闭时,它不会向用户显示异常。)