简体   繁体   中英

What should I do if a IOException is thrown?

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.

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.

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( )
  }
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM