方法中缺少返回语句错误

2022-01-19 00:00:00 return java

我正在尝试编写一个返回我的计算机 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();
        }
    }

推荐答案

所有分支都必须返回一些东西,只需在末尾添加一个return 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 询问最后的 return 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:

    • enter 方法 - 新堆栈帧(1. 在代码中)
      • 进入 try 块(2. 在代码中)
        • 在 try 中处理指令(3. 在代码中)
        • return语句:停止执行block,将值返回到上一个栈帧(代码中的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):

            • enter 方法 - 新堆栈帧(1. 在代码中)
              • 进入 try 块(2. 在代码中)
                • 在 try 中处理指令(3. 在代码中)
                • 抛出异常
                • 进程捕获块(日志异常,6. in code)
                • (现在不是这样,但是如果有一个 finally 块,现在就会执行,甚至会覆盖返回的值...)
                • 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...)

                如您所见,在成功的情况下,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.

相关文章