简体   繁体   English

如果socketChannel.close()得到IOException,该怎么办?

[英]What is the right thing to do if a socketChannel.close() got IOException?

I have a class that wraps socketChannel and has a close() method as follows: 我有一个包装socketChannel的类,并具有如下的close()方法:

public void close() {
    // ... logic ...
    try {
        socketChannel.close();
    } catch (IOException e) {
          // ???
    }
    this.isConnected = false;
}

I want in the end of this operation that socketChannel will be closed and not registered to its selector. 我希望在此操作结束时,将关闭socketChannel而不将其注册到其选择器。 I read and found that the above code is sufficient for that, but what happens if I got an IOException? 我阅读并发现上面的代码足以满足要求,但是如果得到IOException,会发生什么?

My feeling is that "swallowing" it is enough, but am i missing something? 我的感觉是“吞咽”就足够了,但是我错过了什么吗?

The answer will depend on whether it matters that the close threw an exception. 答案将取决于结案引发异常是否重要。 And if it matters, the next question is whether you need to do something about it ... other than reporting it. 而且,如果这很重要,那么下一个问题是您是否需要对它做些其他事情……而不是对其进行报告。

Scenario #1. 场景1。

A web server gets an exception when closing the output stream it sent the response on. Web服务器在关闭发送响应的输出流时会遇到异常。 A typical cause is that the user closed his web browser or lost his network connection at the wrong moment. 一个典型的原因是用户在错误的时间关闭了Web浏览器或失去了网络连接。 The server-side exception doesn't matter (to the server / server admin) and is not even worth logging. 服务器端异常无关紧要(对服务器/服务器管理员而言),甚至不值得记录。

Scenario #2. 场景2。

You are doing something that involves talking to multiple servers, and it is important to know that they all "got the message". 您正在做的事情涉及与多台服务器进行通信,因此重要的是要知道它们都“获得了信息”。 If an exception occurs in the close, that may be an indication that that didn't happen. 如果在结束时发生异常,则可能表示未发生异常。 Probably you need to log this. 可能您需要登录。 Maybe you need to tell the servers. 也许您需要告诉服务器。 Maybe you need to cause some enclosing transaction to rollback. 也许您需要使一些封闭的事务回滚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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