Skip to content

Commit 1faa5c8

Browse files
committed
8282686: Add constructors taking a cause to SocketException
Reviewed-by: alanb, xuelei, lancea, dfuchs
1 parent 7194097 commit 1faa5c8

File tree

7 files changed

+90
-25
lines changed

7 files changed

+90
-25
lines changed

src/java.base/share/classes/java/net/DatagramSocket.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1353,9 +1353,7 @@ private static SocketException toSocketException(IOException e) {
13531353
Throwable cause = e.getCause();
13541354
if (cause instanceof SocketException)
13551355
return (SocketException) cause;
1356-
SocketException se = new SocketException(e.getMessage());
1357-
se.initCause(e);
1358-
return se;
1356+
return new SocketException(e.getMessage(), e);
13591357
}
13601358

13611359
/**

src/java.base/share/classes/java/net/SocketException.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -52,4 +52,27 @@ public SocketException(String msg) {
5252
*/
5353
public SocketException() {
5454
}
55+
56+
/**
57+
* Constructs a new {@code SocketException} with the
58+
* specified detail message and cause.
59+
*
60+
* @param msg the detail message.
61+
* @param cause the cause
62+
* @since 19
63+
*/
64+
public SocketException(String msg, Throwable cause) {
65+
super(msg, cause);
66+
}
67+
68+
/**
69+
* Constructs a new {@code SocketException} with the
70+
* specified cause.
71+
*
72+
* @param cause the cause
73+
* @since 19
74+
*/
75+
public SocketException(Throwable cause) {
76+
super(cause);
77+
}
5578
}

src/java.base/share/classes/javax/net/SocketFactory.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -120,12 +120,8 @@ public Socket createSocket() throws IOException {
120120
// The Exception is used by HttpsClient to signal that
121121
// unconnected sockets have not been implemented.
122122
//
123-
UnsupportedOperationException uop = new
124-
UnsupportedOperationException();
125-
SocketException se = new SocketException(
126-
"Unconnected sockets not implemented");
127-
se.initCause(uop);
128-
throw se;
123+
throw new SocketException("Unconnected sockets not implemented",
124+
new UnsupportedOperationException());
129125
}
130126

131127

src/java.base/share/classes/javax/net/ssl/SSLServerSocketFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ class DefaultSSLServerSocketFactory extends SSLServerSocketFactory {
173173
}
174174

175175
private ServerSocket throwException() throws SocketException {
176-
throw (SocketException)
177-
new SocketException(reason.toString()).initCause(reason);
176+
throw new SocketException(reason.toString(), reason);
178177
}
179178

180179
@Override

src/java.base/share/classes/javax/net/ssl/SSLSocketFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ class DefaultSSLSocketFactory extends SSLSocketFactory
263263
}
264264

265265
private Socket throwException() throws SocketException {
266-
throw (SocketException)
267-
new SocketException(reason.toString()).initCause(reason);
266+
throw new SocketException(reason.toString(), reason);
268267
}
269268

270269
@Override

src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -219,9 +219,7 @@ public void send(DatagramPacket p) throws IOException {
219219
} catch (AlreadyConnectedException e) {
220220
throw new IllegalArgumentException("Connected and packet address differ");
221221
} catch (ClosedChannelException e) {
222-
var exc = new SocketException("Socket closed");
223-
exc.initCause(e);
224-
throw exc;
222+
throw new SocketException("Socket closed", e);
225223
}
226224
} finally {
227225
if (bb != null) {
@@ -249,9 +247,7 @@ public void receive(DatagramPacket p) throws IOException {
249247
p.setSocketAddress(sender);
250248
}
251249
} catch (ClosedChannelException e) {
252-
var exc = new SocketException("Socket closed");
253-
exc.initCause(e);
254-
throw exc;
250+
throw new SocketException("Socket closed", e);
255251
} finally {
256252
Util.offerFirstTemporaryDirectBuffer(bb);
257253
}
@@ -481,7 +477,7 @@ public void joinGroup(InetAddress group) throws IOException {
481477
joinGroup(new InetSocketAddress(group, 0), null);
482478
} catch (IllegalArgumentException iae) {
483479
// 1-arg joinGroup does not specify IllegalArgumentException
484-
throw (SocketException) new SocketException("joinGroup failed").initCause(iae);
480+
throw new SocketException("joinGroup failed", iae);
485481
}
486482
}
487483

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8282686
27+
* @summary Verify cause and message handling of SocketException
28+
*/
29+
import java.net.SocketException;
30+
import java.util.Objects;
31+
32+
public class TestSocketExceptionCtor {
33+
public static void main(String... args) {
34+
String message = "message";
35+
Throwable cause = new RuntimeException();
36+
37+
testException(new SocketException(cause), cause.toString(), cause);
38+
testException(new SocketException(message, cause), message, cause);
39+
}
40+
41+
private static void testException(SocketException se,
42+
String expectedMessage,
43+
Throwable expectedCause) {
44+
var message = se.getMessage();
45+
if (!Objects.equals(message, expectedMessage)) {
46+
throw new RuntimeException("Unexpected message " + message);
47+
}
48+
49+
var cause = se.getCause();
50+
if (cause != expectedCause) {
51+
throw new RuntimeException("Unexpected cause");
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)