java - Why I can't return nothing in a static method this way -
i wrote piece of code train litte bit , wanted understand why cannot use notation.
public static double retanglearea (double c, double d) { double area = c * d; if ( c < 0 || d < 0) { system.out.println("error."); return; } return area; }
if call method in main() shout "error." , still calculate (c * d) negative number wanted print show in. well, know how rid of problem want understand why can't way , what's problem that.
three ways round this:
throw
exception rather writing error console. write console incatch
block arguably in better position deal error.change function return type
double
(note capital), , returnnull
if don't want calculate value.use
java.lang.optional<t>
return type.
of these, prefer , therefore recommend 1.
Comments
Post a Comment