Missing return statement error in a method

后端 未结 1 814
误落风尘
误落风尘 2021-01-28 09:32

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-i

相关标签:
1条回答
  • 2021-01-28 10:18

    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:

    • exceptions - do you really just want to print them on System.err?
      • do you want to print them at all, if you just need the address if it is valid?
    • would you rather have a String returned representing the condition that it was not successful ot obtain the MAC address?

    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 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...)
    • execution of the method that called continues with returned value

    In unsuccessful case, (UnknownHostException for example):

    • enter method - new stack frame (1. in code)
      • enter try block (2. in code)
        • process instructions in try (3. in code)
        • exception thrown
      • enter catch block (5. in code)
        • 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 statement: stop execution of block, the null value is returned to the previous stack frame (7. in code)
    • execution of the method that called continues with returned value

    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).

    The finally block is tricky though: read up on that, it will be useful knowledge.

    0 讨论(0)
提交回复
热议问题