简体   繁体   English

Java错误“重写的方法不会引发java.io.IOException”?

[英]Java error “overridden method does not throw java.io.IOException”?

This may be a very newbie question, but Is there a way to start a thread that creates sockets? 这可能是一个非常新手的问题,但是有没有办法启动创建套接字的线程? Because I can't do the following: 因为我无法执行以下操作:

public void run() throws IOException{

  Socket sock2 = new Socket("localhost", 9898);


}

and I get this error: 我得到这个错误:

run() in CapitalizeClient cannot implement run() in java.lang.Runnable overridden method does not throw java.io.IOException CapitalizeClient中的run()无法实现java.lang.Runnable中的run()重写的方法不会引发java.io.IOException

Or should I just create the socket code in the main? 还是我应该只在主目录中创建套接字代码? I'd like to periodically create sockets to use as a heartbeat. 我想定期创建套接字以用作心跳。 thanks 谢谢

You cannot put throws IOException in the run method because the Runnable interface does not say that the run method throws any checked exceptions. 您不能在run方法中throws IOException ,因为Runnable接口不会说run方法引发任何检查的异常。

Either handle the IOException by catching it, or move the code to another location. 通过捕获IOException来处理它,或者将代码移到另一个位置。

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

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