How to exit void function in Android?

Hope the system can exit the void function HandlePostwhen condition 1 is encountered, so I add returnafter DoSomething1().

But Android Studio will tell me that the "return" information is redundant as the last statement in the "void" method!

private void HandlePost(IHTTPSession session){
    try {

        Map<String, String> files = new HashMap<String, String>();
        session.parseBody(files);

        //Case 1
        String deleteValue=session.getParms().get("ActionDelete");
        if (deleteValue!=null){
           DoSomething1();
           return;
        }

        //Case 2
        String copyValue=session.getParms().get("ActionCopy");
        if (copyValue!=null){
           DoSomething2();
            return;
        }

    } catch (Exception e) {
        Utility.LogError("This is an error "+e.getMessage() );
    }
}
+4
source share
3 answers

An operator is returnnot needed if it is the last (executed) operator in the method. You can either ignore the warning or delete the instruction. Note. The first returnin your example is necessary, but the second is not.

, 2 else.

+3

, return , , , .

, , return, . , - .

.

+1

return;

DoSomething2();

Lint

0

Source: https://habr.com/ru/post/1661153/


All Articles