Discuss / Java / 是否存在代码逻辑问题

是否存在代码逻辑问题

Topic source

```java

    @Override
    public void run() {
        try (InputStream input = this.sock.getInputStream()) {
            try (OutputStream output = this.sock.getOutputStream()) {
                handle(input, output);
            }
        } catch (Exception e) {
            try {
                this.sock.close();
            } catch (IOException ioe) {
            }
            System.out.println("client disconnected.");
        }
    }

```

应该调整输出`client disconnected`位置, 不然非异常退出的时候, 监听不到客户端断开链接, 应改为:

```java

    @Override
    public void run() {
        try (InputStream input = this.sock.getInputStream()) {
            try (OutputStream output = this.sock.getOutputStream()) {
                handle(input, output);
            }
        } catch (Exception e) {
            try {
                this.sock.close();
            } catch (IOException ioe) {
            }
        }
        System.out.println("client disconnected.");
    }

```


  • 1

Reply