JDK 1.1 の新しい SocketExceptions
以前は、Java のすべてのネットワークエラーは SocketException を発生させていましたが、エラーの原因については十分な情報を提供しませんでした。たとえば、リモートコンピュータが接続を拒否したのか (そのポート上でなにも待機していないのか)、またはホストに到達できなかったのか (接続の試みがタイムアウトになったのか) といった原因を特定できませんでした。JDK 1.1 は洗練されたエラーレポートを提供する 3 つの新しいクラスを追加します。
public class BindException extends SocketException {
...
}
public class ConnectException extends SocketException {
...
}
public class NoRouteToHostException extends SocketException {
...
}
import java.net.*;
...
Socket s = null;
try {
s = new Socket("foo.org", 80);
} catch (UnknownHostException e) {
// check spelling of hostname
} catch (ConnectException e) {
// connection refused - is server down? Try another port.
} catch (NoRouteToHostException e) {
// The connect attempt timed out. Try connecting through a proxy
} catch (IOException e) {
// another error occurred
}
import java.net.*;
...
ServerSocket ss = null;
try {
/* try to bind to local address 129.144.175.156 */
InetAddress in = InetAddress.getByName("129.144.175.156");
int port = 8000;
int backlog = 5;
ss = new ServerSocket(port, backlog, in);
} catch (BindException e) {
// port 8000 in use, or can't bind to 129.144.175.156 as a local address
} catch (SocketException e) {
// another error occurred
}