I have the following code:
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
The code is taken from a java.sun.com.
I have several questions concerning the above given short part of the code.
-
Why do we want to catch an IOException. I though that IOException is something that can happen when we use input-output methods (not networking methods).
-
What is the difference between the “System.err.println” and “System.println”?
-
In the catch statement we have “e”. What for? Do we use it latter?
Networking is also input/output. Byte streams through a socket.
The first writes to stderr, the second doesn’t exist.
To have a reference to the exception so that you can if necessary log or rethrow it.