DatagramSocket

沁海棠 2017-02-21 01:01:54
把DatagarmSocket做为类属性并直接构造,为什么会报错。求解,Default constructor cannot handle exception type SocketException thrown by implicit super constructor. Must define an explicit constructor
写在方法里面就可以了。

public class Server {
DatagramSocket serber = new DatagramSocket(6666);
}


public class Server {

public void reception(){
try {
DatagramSocket server = new DatagramSocket(6666);
} catch (SocketException e) {
e.printStackTrace();
}
}
}
求详解
...全文
142 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
oO临时工Oo 2017-02-21
  • 打赏
  • 举报
回复
new Server()完成前,会new DatagramSocket(6666);而new DatagramSocket(6666);失败会导致new Server()失败。 try { DatagramSocket server = new DatagramSocket(6666); } catch (SocketException e) { e.printStackTrace(); } 上面这样,就算new DatagramSocket(6666);报异常,也有try-catch机制,new Server()总会成功。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧