Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8022126

Remove throws SocketException from DatagramPacket constructors accepting SocketAddress

    XMLWordPrintable

Details

    • b105
    • Verified

    Description

      Two DatagramPacket constructors declare that they throw SocketException.

           DatagramPacket(byte[] buf, int len, SocketAddress sa) throws SocketException
           DatagramPacket(byte[] buf, int off, int len, SocketAddress sa) throws SocketException

      As it happens 'throws SE' was incorrectly added to these constructors when introduced in 1.4. The original API specified that SE was thrown when the given SocketAddress was not supported. That was later changed to throw IAE, in 1.4.2. These constructor now can never throw SE.

      Removing 'throws SE' from the method declaration is a binary compatible change, but not source compatible ( XXX is never thrown in body of corresponding try statement ).

      see discussion on:
        http://mail.openjdk.java.net/pipermail/net-dev/2013-July/006889.html
        (cont'd) http://mail.openjdk.java.net/pipermail/net-dev/2013-August/006914.html

      The conclusion of the discussion is that since these constructors are not that widely used (the InetAddress+port variants are popular) . Where they are, the affected code typically sends the packet, which requires handling of IOException anyway.

      For completeness, here is a simple test that demonstrates the source incompatibility.

       : cat ThrowingCtrs.java
      import java.net.*;
      import java.io.IOException;

      public class ThrowingCtrs {

          public static void main(String[] args) {
              SocketAddress sa = new InetSocketAddress("127.0.0.1", 45678);

              // DatagramPacket(byte[], int, SocketAddress)
              try { DatagramPacket dp = new DatagramPacket(new byte[1], 1, sa); }
              catch (SocketException x) { }

              try { DatagramPacket dp = new DatagramPacket(new byte[1], 1, sa); }
              catch (IOException x) { }

              try { DatagramPacket dp = new DatagramPacket(new byte[1], 1, sa); }
              catch (Exception x) { }

              // DatagramPacket(byte[], int, int, SocketAddress)
              try { DatagramPacket dp = new DatagramPacket(new byte[1], 0, 1, sa); }
              catch (SocketException x) { }

              try { DatagramPacket dp = new DatagramPacket(new byte[1], 0, 1, sa); }
              catch (IOException x) { }

              try { DatagramPacket dp = new DatagramPacket(new byte[1], 0, 1, sa); }
              catch (Exception x) { }
          }
      }

      : build/solaris-x86_64-normal-server-release/jdk/bin/javac ThrowingCtrs.java
      ThrowingCtrs.java:11: error: exception SocketException is never thrown in body of corresponding try statement
              catch (SocketException x) { }
              ^
      ThrowingCtrs.java:14: error: exception IOException is never thrown in body of corresponding try statement
              catch (IOException x) { }
              ^
      ThrowingCtrs.java:21: error: exception SocketException is never thrown in body of corresponding try statement
              catch (SocketException x) { }
              ^
      ThrowingCtrs.java:24: error: exception IOException is never thrown in body of corresponding try statement
              catch (IOException x) { }
              ^
      4 errors

      Attachments

        Activity

          People

            chegar Chris Hegarty
            chegar Chris Hegarty
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: