【发布时间】:2011-09-26 18:54:17
【问题描述】:
InputStream in = ClientSocket.getInputStream();
new Thread()
{
public void run() {
while (true)
{
int i = in.read();
handleInput(i);
}
}
}.start();
我正在使用此代码在套接字上收听新数据并得到:
FaceNetChat.java:37: unreported exception java.io.IOException; must be caught or declared to be thrown
int i = in.read();
^
当我在“run()”之后添加“throws IOException”时,我得到:
FaceNetChat.java:34: run() in cannot implement run() in java.lang.Runnable; overridden method does not throw java.io.IOException
public void run() throws IOException {
^
这可能很简单,但我不知所措。我如何通过这个?
【问题讨论】:
-
您希望您的程序如何处理异常?
-
@SomeBloke,实现 Runnable 并将其传递给线程而不是子类化线程被认为是最佳实践。
标签: java sockets exception runnable