Java: 无法初始化 InetAddress
Java: Cannot initialize InetAddress
出于某种原因,我似乎无法初始化 InetAddress 对象,我查看了文档,它与我的使用方式完全一样。
InetAddress firstMachineAddress = InetAddress.getByName("129.26.70.95");
InetAddress secondMachineAddress = InetAddress.getByName("129.26.70.108");
Eclipse 说:
Default constructor cannot handle exception type UnknownHostException thrown by implicit super constructor. Must define an explicit constructor
这到底是怎么回事?
您是否正在处理您调用的方法抛出的 UnknownHostException
?
InetAddress firstMachineAddress;
try {
firstMachineAddress = InetAddress.getByName("129.26.70.95");
} catch (UnknownHostException e) {
// print exception, throw error,
// try something else etc.
}
您的代码似乎在 class 的构造函数中,该构造函数由另一个 class 扩展,如下所示:
import java.net.*;
class SuperclassWithUnknownHostException {
public SuperclassWithUnknownHostException() throws UnknownHostException {
InetAddress firstMachineAddress = InetAddress.getByName("129.26.70.95");
InetAddress secondMachineAddress = InetAddress.getByName("129.26.70.108");
}
}
class SubclassCannotHandleException extends SuperclassWithUnknownHostException {
}
您需要将默认构造函数添加到引发异常的子class:
class SubclassCannotHandleException extends SuperclassWithUnknownHostException {
public SubclassCannotHandleException() throws UnknownHostException {
}
}
inetAddress 对象抛出一个异常,因此您需要围绕它包装一个 try-catch 或让您的方法也抛出异常。另外我相信你会收到错误,因为有一个你没有处理的异常返回。 getByName() 方法使用网址名称的字符串参数,例如 www.google.com 或 www.amazon.com 等等。
try{
InetAddress Address = InetAddress.getByName("www.google.com");
System.out.println(Address);
}catch(UnknownHostException e){
e.printStackTrace();
}
这显示 www.google.com/64.233.177.147
出于某种原因,我似乎无法初始化 InetAddress 对象,我查看了文档,它与我的使用方式完全一样。
InetAddress firstMachineAddress = InetAddress.getByName("129.26.70.95");
InetAddress secondMachineAddress = InetAddress.getByName("129.26.70.108");
Eclipse 说:
Default constructor cannot handle exception type UnknownHostException thrown by implicit super constructor. Must define an explicit constructor
这到底是怎么回事?
您是否正在处理您调用的方法抛出的 UnknownHostException
?
InetAddress firstMachineAddress;
try {
firstMachineAddress = InetAddress.getByName("129.26.70.95");
} catch (UnknownHostException e) {
// print exception, throw error,
// try something else etc.
}
您的代码似乎在 class 的构造函数中,该构造函数由另一个 class 扩展,如下所示:
import java.net.*;
class SuperclassWithUnknownHostException {
public SuperclassWithUnknownHostException() throws UnknownHostException {
InetAddress firstMachineAddress = InetAddress.getByName("129.26.70.95");
InetAddress secondMachineAddress = InetAddress.getByName("129.26.70.108");
}
}
class SubclassCannotHandleException extends SuperclassWithUnknownHostException {
}
您需要将默认构造函数添加到引发异常的子class:
class SubclassCannotHandleException extends SuperclassWithUnknownHostException {
public SubclassCannotHandleException() throws UnknownHostException {
}
}
inetAddress 对象抛出一个异常,因此您需要围绕它包装一个 try-catch 或让您的方法也抛出异常。另外我相信你会收到错误,因为有一个你没有处理的异常返回。 getByName() 方法使用网址名称的字符串参数,例如 www.google.com 或 www.amazon.com 等等。
try{
InetAddress Address = InetAddress.getByName("www.google.com");
System.out.println(Address);
}catch(UnknownHostException e){
e.printStackTrace();
}
这显示 www.google.com/64.233.177.147