下面的代码来自this答案“不能返回为void结果类型的值”的错误
try {
// get all the interfaces
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
//find network interface wlan0
for (NetworkInterface networkInterface : all) {
if (!networkInterface.getName().equalsIgnoreCase("wlan0")) continue;
//get the hardware address (MAC) of the interface
byte[] macBytes = networkInterface.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
//gets the last byte of b
res1.append(Integer.toHexString(b & 0xFF) + ":");
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
ex.printStackTrace();
}
我得到的那些2线错误Cannot return a value with void result type
:return "";
和return res1.toString();
我把里面的代码public void onStart()
我该如何解决这个,你能告诉我这个问题的原因吗?
那么如果try块引发异常会发生什么?那个回报在哪里? – SoroushA
我的猜测是,你有一个void方法内的代码,并且void方法不返回任何东西 –