java - Is it okay to throw NullPointerException programmatically? -


when there post-condition, return value of method must not null, can done?

i do

assert returnvalue != null : "not acceptable null value"; 

but assertions turned off!

so okay do

if(returnvalue==null)       {            throw new nullpointerexception("return value null @ method aaa");       } 

?

or better use user-defined exception (like nullreturnvalueexception ) such condition?

i see no problem throwing npe possible before jvm - in particular null arguments. there seems debate this, there many examples in java se libraries this. cannot see why npe should holy in aspect not able throw yourself.

however, digress. question different. talking post-condition stating return value mustn't null. surely null in case mean have bug inside method?

how document this? "this method throws nullpointerexception if return value unexpectedly null"? without explaining how happen? no, use assertion here. exceptions should used errors can conceivably happen - not cover things can happen if there's wrong inside method, because not anybody.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -