Skip to content
Permalink
Browse files
8282686: Add constructors taking a cause to SocketException
Reviewed-by: alanb, xuelei, lancea, dfuchs
  • Loading branch information
jddarcy committed Mar 7, 2022
1 parent 7194097 commit 1faa5c8092f8baec3ca08ed059653876ec46db46
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 25 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -1353,9 +1353,7 @@ private static SocketException toSocketException(IOException e) {
Throwable cause = e.getCause();
if (cause instanceof SocketException)
return (SocketException) cause;
SocketException se = new SocketException(e.getMessage());
se.initCause(e);
return se;
return new SocketException(e.getMessage(), e);
}

/**
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -52,4 +52,27 @@ public SocketException(String msg) {
*/
public SocketException() {
}

/**
* Constructs a new {@code SocketException} with the
* specified detail message and cause.
*
* @param msg the detail message.
* @param cause the cause
* @since 19
*/
public SocketException(String msg, Throwable cause) {
super(msg, cause);
}

/**
* Constructs a new {@code SocketException} with the
* specified cause.
*
* @param cause the cause
* @since 19
*/
public SocketException(Throwable cause) {
super(cause);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -120,12 +120,8 @@ public Socket createSocket() throws IOException {
// The Exception is used by HttpsClient to signal that
// unconnected sockets have not been implemented.
//
UnsupportedOperationException uop = new
UnsupportedOperationException();
SocketException se = new SocketException(
"Unconnected sockets not implemented");
se.initCause(uop);
throw se;
throw new SocketException("Unconnected sockets not implemented",
new UnsupportedOperationException());
}


@@ -173,8 +173,7 @@ class DefaultSSLServerSocketFactory extends SSLServerSocketFactory {
}

private ServerSocket throwException() throws SocketException {
throw (SocketException)
new SocketException(reason.toString()).initCause(reason);
throw new SocketException(reason.toString(), reason);
}

@Override
@@ -263,8 +263,7 @@ class DefaultSSLSocketFactory extends SSLSocketFactory
}

private Socket throwException() throws SocketException {
throw (SocketException)
new SocketException(reason.toString()).initCause(reason);
throw new SocketException(reason.toString(), reason);
}

@Override
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -219,9 +219,7 @@ public void send(DatagramPacket p) throws IOException {
} catch (AlreadyConnectedException e) {
throw new IllegalArgumentException("Connected and packet address differ");
} catch (ClosedChannelException e) {
var exc = new SocketException("Socket closed");
exc.initCause(e);
throw exc;
throw new SocketException("Socket closed", e);
}
} finally {
if (bb != null) {
@@ -249,9 +247,7 @@ public void receive(DatagramPacket p) throws IOException {
p.setSocketAddress(sender);
}
} catch (ClosedChannelException e) {
var exc = new SocketException("Socket closed");
exc.initCause(e);
throw exc;
throw new SocketException("Socket closed", e);
} finally {
Util.offerFirstTemporaryDirectBuffer(bb);
}
@@ -481,7 +477,7 @@ public void joinGroup(InetAddress group) throws IOException {
joinGroup(new InetSocketAddress(group, 0), null);
} catch (IllegalArgumentException iae) {
// 1-arg joinGroup does not specify IllegalArgumentException
throw (SocketException) new SocketException("joinGroup failed").initCause(iae);
throw new SocketException("joinGroup failed", iae);
}
}

@@ -493,7 +489,7 @@ public void leaveGroup(InetAddress group) throws IOException {
leaveGroup(new InetSocketAddress(group, 0), null);
} catch (IllegalArgumentException iae) {
// 1-arg leaveGroup does not specify IllegalArgumentException
throw (SocketException) new SocketException("leaveGroup failed").initCause(iae);
throw new SocketException("leaveGroup failed", iae);
}
}

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8282686
* @summary Verify cause and message handling of SocketException
*/
import java.net.SocketException;
import java.util.Objects;

public class TestSocketExceptionCtor {
public static void main(String... args) {
String message = "message";
Throwable cause = new RuntimeException();

testException(new SocketException(cause), cause.toString(), cause);
testException(new SocketException(message, cause), message, cause);
}

private static void testException(SocketException se,
String expectedMessage,
Throwable expectedCause) {
var message = se.getMessage();
if (!Objects.equals(message, expectedMessage)) {
throw new RuntimeException("Unexpected message " + message);
}

var cause = se.getCause();
if (cause != expectedCause) {
throw new RuntimeException("Unexpected cause");
}
}
}

1 comment on commit 1faa5c8

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.