Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 898
Roman Avatar asked Mar 23 '10 11:03

Roman


People also ask

How do you deal with IOException?

IOException is a Java exception that occurs when an IO operation fails. Develop can explicitly handle the exception in a try-catch-finally block and print out the root cause of the failure. The developer can take the correct actions to solve this situation by having additional code in the catch and finally blocks.

What does throw IOException mean?

The throws keyword indicates that a certain method can potentially "throw" a certain exception. You need to handle a possible IOException (and possibly other exceptions) either with a try-catch block or by adding throws IOException, (...) to your method declaration.

What happens when an exception is thrown?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.

How do I fix Java IO IOException broken pipe?

io. IOException: Broken pipe changing the value will help until the root cause (60s should be enough) can be fixed. Show activity on this post. Basically, what is happening is that your user is either closing the browser tab, or is navigating away to a different page, before communication was complete.


3 Answers

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.
like image 158
TomTom Avatar answered Oct 20 '22 17:10

TomTom


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( )
  }
}
like image 38
Alexander Pogrebnyak Avatar answered Oct 20 '22 19:10

Alexander Pogrebnyak


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.
like image 23
Chris Nava Avatar answered Oct 20 '22 17:10

Chris Nava