2013-09-23 105 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