I'm working on some server code for a program I'm developing and I'm using try-with-resource statements to close the sockets.
try (
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
) {
out.println("This is the server! Hooray! :D");
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
The problem is, what if the user decides to close the program before the try block finishes? Will the .close() methods be called?
This is on oracle's website:it will be closed regardless of whether the try statement completes normally or abruptly
But I'm not sure if that includes if the user just closes the program before the try finishes or just if the program crashes.
Thanks.