2

The file is java.nio.channels.SocketChannel.java. JDK 7u45. The excerpt is:

public static SocketChannel open(SocketAddress remote)
    throws IOException
{
    SocketChannel sc = open();
    try {
        sc.connect(remote);
    } catch (Throwable x) {
        try {
            sc.close();
        } catch (Throwable suppressed) {
            x.addSuppressed(suppressed);
        }
        throw x;
    }
    assert sc.isConnected();
    return sc;
}

How does the compiler let passed that code? The signature declares IOException, but the method's body catches Throwable and retrows it. What don't I understand?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
danissimo
  • 327
  • 3
  • 10
  • Please do not add answers to the question body itself. Instead, you should add it as an answer. [Answering your own question is allowed and even encouraged](https://stackoverflow.com/help/self-answer). – Adriaan Jan 16 '23 at 11:16

1 Answers1

1

What you don't understand is that the compiler only checks checked exceptions, i.e. those that derive from Exception excluding those that derive from RuntimeException. Exceptions coming from elsewhere in the hierarchy starting at Throwable aren't subject to compilation rules.

user207421
  • 305,947
  • 44
  • 307
  • 483