下面的代碼來自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方法不返回任何東西 –