简体   繁体   English

当我将SocketException扔回IOException时,为什么Java会抱怨?

[英]Why does java complain when I rethrow SocketException to be caught by IOException?

The general rule of exceptions is that you catch more specific ones before catching general ones. 一般的例外规则是,在捕获一般异常之前,您要捕获更具体的异常。 I have a case where closing a server throws SocketException: socket closed coming from my listener which is caught by IOException , but I don't want to show the user that message when they close the server. 我有一种情况,关闭服务器时会抛出SocketException: socket closed是由IOException捕获的来自我的侦听器的,但是我不想在用户关闭服务器时向用户显示该消息。 Everything else is probably an actual error so it should be shown to them, so I catch SocketException , check its message and if it is not socket closed then it should be rethrown to be caught and handled as an IOException . 其他所有内容都可能是实际错误,因此应该向他们显示,因此我捕获了SocketException ,检查其消息,如果未socket closed ,则应重新IOExceptionIOException ,以作为IOException进行捕获和处理。 Java/NetBeans 7.0.1 do not seem to like that. Java / NetBeans 7.0.1似乎不喜欢这样。 Here is my code: 这是我的代码:

public void run() {
        while (runner == Thread.currentThread()) {
            System.out.println("waiting for connection...");
            try {
                Socket s = server.accept(); //throws SocketException/IOException
                if (session == null) {
                    session = new ReceiveSession(s, parent);
                } else {
                    s.close();
                }
            } catch (SocketException e) {
                if (!e.getMessage().equals("socket closed")) {
                    throw e; //error line, "unreported exception SocketException"
                }
            } catch (IOException e) {
                e.printStackTrace();
                parent.showError("Someone tried to connect but the connection failed: " + e);
                session = null;
            }
        }

    }

When trying to clean and build, I get: 尝试清洁和构建时,我得到:

error: unreported exception SocketException; must be caught or declared to be thrown
                throw e;
1 error

Since SocketException extends IOException , it should be caught by the more general IOException . 由于SocketException扩展了IOException ,因此应该由更通用的IOException捕获。 Why do I get this error? 为什么会出现此错误? (Running the project as is in NetBeans works perfectly btw, it doesn't show the user the exception when the server shuts down as I want.) (顺便说一下,在NetBeans中运行项目的效果很好,顺便说一句,当服务器关闭时,它不会向用户显示异常。)

您的throwtry之外 ,因此,下面的catch不会处理引发的异常。

The second catch will not catch the rethrow since it wasn't thrown in the try block for which the second catch is responsible. 第二个catch不会捕获该重新抛出,因为第二个catch并没有将其抛出在try块中。 You'll need to restructure the code. 您需要重组代码。


For example, the most straight forward restructuring would be this: 例如,最直接的重组是这样的:

public void run() {
    while (runner == Thread.currentThread()) {
        System.out.println("waiting for connection...");
        try {
            try {
                Socket s = server.accept(); //throws SocketException/IOException
                if (session == null) {
                    session = new ReceiveSession(s, parent);
                } else {
                    s.close();
                }
            } catch (SocketException e) {
                if (!e.getMessage().equals("socket closed")) {
                    throw e; //error line, "unreported exception SocketException"
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            parent.showError("Someone tried to connect but the connection failed: " + e);
            session = null;
        }
    }
}

The nested tries make me cry a little, but it would get the job done (and to be honest, off the top of my head, I can't think of a better way that doesn't go way too far). 嵌套的尝试使我哭了一下,但是可以完成工作(老实说,我的头顶上,我想不出一个更好的方法,不会走得太远)。

Catch statements catch exceptions from their respective try block, not from other catch blocks in the list. Catch语句从其各自的try块捕获异常,而不从列表中的其他catch块捕获异常。 You're throwing the exception from outside the try, so it does not get caught by subsequent catch blocks. 您从尝试之外抛出了异常,因此后续捕获块不会捕获该异常。

暂无
暂无

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

相关问题 抛出Java中不同的捕获异常 - Rethrow different caught exceptions in java 处理请求时捕获到 I/O 异常 (java.net.SocketException) - Getting I/O exception (java.net.SocketException) caught when processing request 连接到目标主机时捕获到I / O异常(java.net.SocketException):打开的文件太多 - I/O exception (java.net.SocketException) caught when connecting to the target host: Too many open files Tomcat 7 maven 插件 - 处理请求时捕获的 I/O 异常 (java.net.SocketException):管道损坏 - Tomcat 7 maven plugin - I/O exception (java.net.SocketException) caught when processing request: Broken pipe Java:为什么编译器会在这里抱怨? - Java: Why does the compiler complain here? 为什么此代码会与“套接字设置捕获的IOexception”一起循环 - Why does this code loop around with “Socket setup caught IOexception” Tomcat7 maven 插件 - 处理请求时捕获 I/O 异常 (java.net.SocketException):管道损坏 - Tomcat7 maven plugin - I/O exception (java.net.SocketException) caught when processing request: Broken pipe 从 ResultSet 读取 Sybase 列时,我得到“JZ006: Caught IOException: java.io.IOException: JZ0R3: Column is DEAD”。 - When reading Sybase column from ResultSet I get “JZ006: Caught IOException: java.io.IOException: JZ0R3: Column is DEAD.” 当我尝试添加更多代码时,为什么编译器会抱怨“虽然期望”? - Why does the compiler complain “while expected” when I try to add more code? 如何抛出被捕获的异常 - How to rethrow caught exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM