SocketException Handling

I can`t understand something simple. I have a class that handles the socket input. I have a catch-clause:

    public class EntryPoint implements Runnable {

        private Socket socket = null;
        private BufferedReader br = null; // receives data from the destination

...

        public void run() {     
            String command = null;      // buffer for holding one request from command line
            StringReader commandReader = null; // stream for reading command
            try {
                while (!socket.isClosed() && (command = br.readLine()) != null) {               
                    try {
                        command = command.trim();
                        commandReader = new StringReader(command);
                        Request req = JAXB.unmarshal(commandReader, Request.class); 
                        commandReader.close();  
                        dispatcher.sendRequest(req);                                        
                    } catch(DataBindingException ex) {
                        response.sendResponse(SystemMessageFactory.INVALID);
                        response.sendResponse(SystemMessageFactory.SOCKET_SHUTDOWN);
                    }                       
                }
            } catch (SocketException e) {
                System.out.println("Socket Exception");
            } catch (IOException e) {
                Logger.getLogger("server").log(Level.SEVERE, 
                        "Error reading the command input of the client!", e);
            } 
        }       

    }

When the peer abruptly shuts down the socket, the connection reset is sent. The stack trace is:

16.07.2013 1:39:51 EntryPoint run
SEVERE: Error reading the command input of the client!
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at blood.steel.server.EntryPoint.run(EntryPoint.java:36)
    at java.lang.Thread.run(Thread.java:662)

How is it possible? I am catching it twice! SocketException is caught in its own catch-clause and in IOException catch clause. But nothing happens! It does not catch the socket exception. How can I handle it and what is the cause of such behaviour?


Either the SocketExceptioon is not the one in java.net. Check your imports.

Or you are not running the code you think you are.

链接地址: http://www.djcxy.com/p/56570.html

上一篇: HttpURLConnection连接重置错误处理

下一篇: SocketException处理