In Java 8, java.net.DatagramPacket constructors that accept a
java.net.SocketAddress argument removed the declaration that a
SocketException can be thrown.
Prior to Java 8, java.net.DatagramPacket constructors declared
that a java.net.SocketException can be thrown, but the exception was never thrown by those constructors.
This rule flags DatagramPacket constructors that include a
SocketAddress argument that are surrounded by a try block that catches
SocketException or its superclass java.io.IOException, as shown in the following example:
public DatagramPacket getDatagramPacket(SocketAddress socketAddress,
byte[] bytes) {
DatagramPacket dp = null;
try {
dp = new DatagramPacket(bytes, bytes.length, socketAddress);
} catch (SocketException e) {
e.printStackTrace();
}
return dp;
}
The following DatagramPacket constructors are affected:
public DatagramPacket(byte[] buf,
int offset,
int length,
SocketAddress address)
public DatagramPacket(byte[] buf,
int length,
SocketAddress address)
If no other code within the try block throws a
SocketException, when you compile with Java 8 an error
will indicate that the exception is unreachable.
Remove the catch block so that the error does not occur.
For more information about the java.net.DatagramPacket
class,
see the
Class java.net.DatagramPacket Java documentation.