Java处理异常功能编程

发布于 2025-02-03 03:29:18 字数 570 浏览 2 评论 0原文

我有这个代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2025-02-10 03:29:18

在不知道更多的情况下,我会尝试使用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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文