Merl's Blog

Try Again

Tinkering with your project causes interesting side effects.

In short, practicing TDD will save you from yourself but that is a post for another day.

Yesterday I was having some unexpected behavior when I was displaying my server to some people.

My server kept loading, when before it didn’t.

My server would go to most directories and files, But a few specific ones it would not bring up at all until I manually ended the server.

Lastly, when I tried to go into other directories that didn’t exist or I shouldn’t have access to, it would stay loading.

I found my mistake..


try (Socket socket = server.accept()) {
//stuff }

In this example, the server.accept() is doing more than just trying.

Here it is trying and closing at the same time since it is located in the try statement.

I moved it down to here:

try  {
Socket socket = server.accept()
//stuff
}

This is not the same!

I thought it would be, but it is not!

This was the cause of all those problems above.

What I forgot to do if i placed the accept() call in the try box was to close it.

```java finally{ Socket.close //along with the streams as well }

When I added this code in here it got rid of all of my problems!

It is good to tinker but make sure you have tests to protect you!

Best,

Merl