The general rule of exceptions is that you catch more specific ones before catching general ones. I have a case where closing a server throws SocketException: socket closed
coming from my listener which is caught by IOException
, but I don't want to show the user that message when they close the server. Everything else is probably an actual error so it should be shown to them, so I catch SocketException
, check its message and if it is not socket closed
then it should be rethrown to be caught and handled as an IOException
. Java/NetBeans 7.0.1 do not seem to like that. Here is my code:
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;
}
}
}
When trying to clean and build, I get:
error: unreported exception SocketException; must be caught or declared to be thrown
throw e;
1 error
Since SocketException
extends IOException
, it should be caught by the more general IOException
. Why do I get this error? (Running the project as is in NetBeans works perfectly btw, it doesn't show the user the exception when the server shuts down as I want.)