Java处理异常功能编程
我有这个代码:
private void handleReceivedMessage(String m) throws IOException {
switch(m) {
case "ping":
// code block
ping();
break;
default:
System.out.println("message not understood");
}
}
public DataServer(int port) throws IOException {
server = new Server(port, this::handleReceivedMessage);
server.acceptClient();
}
现在,我在此行中遇到了错误:: handlereceivedMessage: 未经手的例外:java.io.ioexception
如何解决这个问题?很明显,HandlereCeivedMessage功能确实抛出了IOException
I have this piece of code:
private void handleReceivedMessage(String m) throws IOException {
switch(m) {
case "ping":
// code block
ping();
break;
default:
System.out.println("message not understood");
}
}
public DataServer(int port) throws IOException {
server = new Server(port, this::handleReceivedMessage);
server.acceptClient();
}
now I get the error at the line this::handleReceivedMessage:
Unhandled exception: java.io.IOException
How do I solve this? It's clear that the handleReceivedMessage functions does throw an IOException
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不知道更多的情况下,我会尝试使用Try/Catch语句专门捕获Olivier建议的例外。如果我可以问,为什么不使用尝试/捕获来检查错误?
Without knowing more, I'd try using a try/catch statement specifically to catch the exception being thrown as Olivier is suggesting. If may I ask, why aren't you using try/catch for error checking?