我该如何处理ServerSocket(Channel).accept()
例外?客户端故障是否会抛出此异常?在JavaDoc中只说:
IOException - If some other I/O error occurs
答案 0 :(得分:3)
我应该如何处理ServerSocket(Channel).accept()异常?
关闭频道。如果您应该是容错的,请尝试打开一个新的并继续接受。
客户端故障是否可以抛出此异常?
没有
答案 1 :(得分:1)
你的处理方式完全取决于你和你的程序。我们都看到了一个程序的消息,说明“发生了一个未知的异常”。这可能是IOException。它是传输数据可能产生的未知的其他问题。当然,他们还有其他可以捕获的例外,这些例外更有可能出现,并且可以采用不同的方式处理。
try {
channel.accept();
} catch(NotYetBoundException e) {
// If this channel's socket has not yet been bound
} catch(ClosedByInterruptException e) {
// If another thread interrupts the current thread while the accept operation is in progress, thereby closing the channel and setting the current thread's interrupt status
} catch(AsynchronousCloseException e) {
// If another thread closes this channel while the accept operation is in progress
} catch(ClosedChannelException e) {
// If this channel is closed
} catch(SecurityException e) {
// If a security manager has been installed and it does not permit access to the remote endpoint of the new connection
} catch(IOException e) {
// If some other I/O error occurs
}