HelloCW HelloCW - 15 days ago 5
Java Question

How to quit in a void function in Android?

I hope the system can quit the void function

HandlePost
when meet a condition Case 1, so I add
return
after
DoSomething1()
.

But Android Studio tell me the info 'return' is unncessery as last statement in a '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() );
}
}

Answer

The return statement is unnecessary if it is the last (executed) statement in the method. You can either ignore the warning or remove the statement. Note: The first return in your example is necessary but the second isn't.

Comments