views:

77

answers:

3

I have the following 3 lines of the code:

ServerSocket listeningSocket = new ServerSocket(earPort);
Socket serverSideSocket = listeningSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(serverSideSocket.getInputStream()));

The compiler complains about all of these 3 lines and its complain is the same for all 3 lines: unreported exception java.io.IOException; In more details, these exception are thrown by new ServerSocket, accept() and getInputStream().

I know I need to use try ... catch .... But for that I need to know what this exceptions mean in every particular case (how should I interpret them). When they happen? I mean, not in general, but in these 3 particular cases.

+4  A: 

You dont know IN PARTICULAR because IO Exception is also a "generic" exception that can have many causes technically. It means an unexpected issue around input / output happened, but obviously it has different causes on local hard disc than on the internet.

In general, all three items resolve around sockets. So causes are related to network issues. Possible are:

  • No network at all, not even localhost (would be a serious technical issue).
  • Port already in use, when a port number is given (new Server Socket(earPort))
  • Network issues - for example somseone stumbled over the cable during some stuff. Can also be a cause of bad quality, a DDOS attack etc.
  • Port exhaustion - no client side port available for a new connection.

Basically around this line.

The same will happen or be able to happen whenever you actually do something with the streams.

In thi scase you ahve two possible main causes:

  • First line: the socket is already in use (program started 2 times, same port as other program). This obviously is non-fixable normally unless the user does something.
  • Generic later runtime error. These can happen during normal operations.
TomTom
+1 This is the core of the answer. Think about what can go wrong. The API is telling you that it won't always work out for reasons beyond your control. How do you want to handle that error condition? Once you know the answer to that question, you will know what you want to do to satisfy the compiler.
Yishai
+1  A: 

The simplest way is to declare your calling method to throw IOException, but you need to cleanup allocated resources in finally clauses before you leave your method:

public void doSession ( ) throws IOException
{
  final ServerSocket listeningSocket = new ServerSocket(earPort);

  try
  {
    final Socket serverSideSocket = listeningSocket.accept();

    try
    {
      final BufferedReader in =
        new BufferedReader(
          new InputStreamReader(
            serverSideSocket.getInputStream()
          )
        );
    }
    finally
    {
      serverSideSocket.close( )
    }
  }
  finally
  {
    listeningSocket.close( )
  }
}
Alexander Pogrebnyak
A: 

In general it doesn't matter exactly what caused the initial IOException because there's little your app can do to correct the situation.

However, as a general answer to your question of "what to do" You have a few options.

  • Try Again - May work if the problem was intermittent. Remember to supply a break condition in case it doesn't.
  • Try Something Else - Load the resource from a different location or via a different method.
  • Give Up - Throw/rethrow the exception and/or abort the action or perhaps the entire program. You may want to provide a user friendly message at this point... ;-) If your program requires the input to function then not having the input leaves you little choice but not to function.
Chris Nava