缺少方法中的return语句错误 [英] Missing return statement error in a method
问题描述
我正在尝试编写一个返回计算机MAC地址字符串的静态方法(函数本身在这里找到: http://www.mkyong.com/java/how-to-get-mac-address-in-java/ )。我遇到静态函数的 return
方面的问题。我得到的错误是缺少返回语句
。我如何解决这个问题?
I am trying to write a static method that returns a string of my computer's MAC address (the function itself was found here: http://www.mkyong.com/java/how-to-get-mac-address-in-java/). I am having issues with the return
aspect of the static function. The error I get is the missing return statement
. How do I remedy this?
static String returnMacAddress(){
InetAddress ip;
try{
ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address: ");
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < mac.length; i++){
stringBuilder.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
return stringBuilder.toString();
}catch(UnknownHostException e){
e.printStackTrace();
} catch(SocketException e){
e.printStackTrace();
}
}
推荐答案
全部分支必须返回一些东西,只需在最后添加返回null;
:
All branches must return something, just add a return null;
at the end:
static String returnMacAddress(){ // 1.
InetAddress ip;
try{ // 2.
ip = InetAddress.getLocalHost(); // 3. (until return stmt)
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address: ");
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < mac.length; i++){
stringBuilder.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
return stringBuilder.toString(); // 4.
}catch(UnknownHostException e){ // 5.
e.printStackTrace(); // 6.
} catch(SocketException e){
e.printStackTrace();
}
return null; // 7.
}
这是语法正确 - 但你必须考虑这意味着什么语义,如果这是所需的操作:
This is syntactically correct - but you have to think about what this means semantically, and if that is the desired operation:
- 例外 - 你真的只想在System.err上打印它们吗?
- 如果您只需要地址,是否要打印它们?
编辑
在这种情况下控制如何流动 - 因为OP询问最后返回null
是否会否定之前的值,成功执行:EDIT How the control flows in this case - as OP asked if the
return null
at the end would negate the previous value, in a successful execution:- 输入方法 - 新堆栈帧(代码中为1.)
- 输入try block(2.代码中)
- try中的处理指令(3.代码中)
- return语句:停止执行块,该值返回到前一个堆栈帧(代码中为4.)
- (现在不是这样,但如果有
finally
阻止,现在将执行,甚至可以覆盖返回的值...)
- enter method - new stack frame (1. in code)
- enter try block (2. in code)
- process instructions in try (3. in code)
- return statement: stop execution of block, the value is returned to the previous stack frame (4. in code)
- (not a case now, but if there was a
finally
block, that would be executed now, and that could even overwrite the returned value...)
在不成功的情况下,(例如UnknownHostException):
In unsuccessful case, (UnknownHostException for example):
- 输入方法 - 新堆栈帧(1。在代码中)
- 输入try block(2.代码中)
- 在try中处理指令(3.代码中)
- 抛出异常
- 进程捕获块(日志异常,代码中的6.)
- (现在不是一个案例,但如果最终有一个
- process catch block (log exception, 6. in code)
- (not a case now, but if there was a
finally
block, that would be executed now, and that could even overwrite the returned value...)
如您所见,成功case,
return null;
语句,即使它在实际返回之后,也不会影响返回的值。每当达到返回时,停止当前块的执行。 (如果在实际上下文中有一个,则相应的finally块将获得控件)。As you see, in successful case, the
return null;
statement, even though it is after the "real return", doesn't influence the returned value. Whenever a return is reached, the eecution of the current block is stopped. (and if there is one in the actual context, the appropriate finally block will get the control).finally
阻塞是棘手的:阅读,这将是有用的知识。The
finally
block is tricky though: read up on that, it will be useful knowledge.这篇关于缺少方法中的return语句错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 输入try block(2.代码中)
- enter try block (2. in code)
- 输入try block(2.代码中)