8282686: Add constructors taking a cause to SocketException

Reviewed-by: alanb, xuelei, lancea, dfuchs
This commit is contained in:
Joe Darcy 2022-03-07 17:52:04 +00:00
parent 7194097bca
commit 1faa5c8092
7 changed files with 90 additions and 25 deletions

View File

@ -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 @@ public class DatagramSocket implements java.io.Closeable {
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);
}
/**

View File

@ -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 class SocketException extends IOException {
*/
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);
}
}

View File

@ -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 abstract class SocketFactory
// 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());
}

View File

@ -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

View File

@ -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

View File

@ -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 class DatagramSocketAdaptor
} 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 class DatagramSocketAdaptor
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 class DatagramSocketAdaptor
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 class DatagramSocketAdaptor
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);
}
}

View File

@ -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");
}
}
}