java - What is the purpose of Objects#requireNonNull -


after checking javadocs method thinking of using, requirednonnull, stumbled across first 1 single parameter (t obj).

however actual purpose of particular method signature? throw , npe i'm positive (as may missing obvious here) thrown anyway.

throws: nullpointerexception - if obj null


the latter makes sense in terms of debugging code, doc states, it's designed parameter validation

public static <t> t requirenonnull(t obj,string message)

checks specified object reference not null , throws customized nullpointerexception if is.

therefore can print specific information along npe make debugging hell of lot easier.


with in mind highly doubt come across situation i'd rather use former instead. please enlighten me.

tl;dr - why ever use overload doesn't take message.

a principle when writing software catch errors possible. quicker notice, example, bad value such null being passed method, easier find out cause , fix problem.

if pass null method not supposed receive null, nullpointerexception happen somewhere, noticed. however, exception might not happen until few methods further down, , when happens somewhere deep down, more difficult find exact source of error.

so, it's better when methods check arguments front , throw exception find invalid value such null.

edit - one-parameter version: though won't provide error message, checking arguments , throwing exception more useful letting null pass down until exception happens somewhere deeper down. stack trace point line used objects.requirenonnull(...) , should obvious developer that means you're not supposed pass null. when let nullpointerexception happen implicitly don't know if original programmer had intent variable should not null.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -