Scanner returns null instead throwing an exception

I have trouble with networking in java. I have tried to read a message from a client over sockets. I use BufferedReader for reading the message.

public String read() throws IOException {
    String message = reader.readLine();
    return message;

When I am on reader.readline() method on the server, if the client kills the connection I expect an error actually. However, instead of throwing an exception, it returns NULL.


@Eray Tuncer it depends on when the connection was closed if it is before start reading the line then yes you should expect an exception. but if it is in between reading I think you will get "null" indicating end of the stream. Please check the following implementation of readLine from BufferedReader :

String readLine(boolean ignoreLF) throws IOException { StringBuffer s = null; int startChar;

    synchronized (lock) {
        ensureOpen(); //This method ensures that the stream is open and this is called before start reading

.................. ................ //----Now reading operation started if the connection is closed it will just return a null--------- bufferLoop: for (;;) {

            if (nextChar >= nChars)
            if (nextChar >= nChars) { /* EOF */
                if (s != null && s.length() > 0)
                    return s.toString();
                    return null;
            boolean eol = false;
            char c = 0;
            int i;

            /* Skip a leftover '\n', if necessary */
            if (omitLF && (cb[nextChar] == '\n'))
            skipLF = false;
            omitLF = false;

            for (i = nextChar; i < nChars; i++) {
                c = cb[i];
                if ((c == '\n') || (c == '\r')) {
                    eol = true;
                    break charLoop;

            startChar = nextChar;
            nextChar = i;

            if (eol) {
                String str;
                if (s == null) {
                    str = new String(cb, startChar, i - startChar);
                } else {
                    s.append(cb, startChar, i - startChar);
                    str = s.toString();
                if (c == '\r') {
                    skipLF = true;
                return str;

            if (s == null)
                s = new StringBuffer(defaultExpectedLineLength);
            s.append(cb, startChar, i - startChar);

So bottom line is that you should check for null in this operation rather than relying on an IOException. I hope it will help you to fix your problem. Thank you !

You can trigger an exception manually like this:

public String read() throws IOException {
    String message = reader.readLine();
    if (message == null)
        throw new IOException("reader.readLine() returned null");
    return message;

Need Your Help

Add image button in draggable DIV & toggle image source on click

jquery html css position draggable

I have draggable DIV's. An image is appended in these DIV's. When mouse is over DIV' s, the opacity of the image is changed to 1. When mouse is out, image gets opacity 0.

Dynamic authority and facets


I have a custom dynamic authority and in order to make the search working I disabled the permissions check on Solr ("alfresco.doPermissionChecks=false"). So then, the results are filtered on the