2013-09-23 104 views

回答

3

我应该如何处理一个ServerSocket(信道)。接受()异常?

关闭频道。如果您应该具有容错能力,请尝试打开一个新的并继续接受。

这个异常可以由客户端错误引发吗?

+0

服务器频道或客户频道? – sashok724

+0

你得到了例外。 – EJP

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 
} 
+0

唯一一次号,你会看到“未知异常”是写得不好的代码,构成了自己的消息像,而不是使用异常本身和它自己的消息。 – EJP