Should I throw a NullPointerException explicitly or let Java do it for me?

前端 未结 6 2102
一整个雨季
一整个雨季 2021-02-19 13:23

As the title says, I am wondering what the best practice is regarding the throwing of NullPointerExceptions. Specifically, if I have an external library function that can return

6条回答
  •  北海茫月
    2021-02-19 13:27

    Very interesting question.

    I think some kind of ProblemCreatingSynthesizerException should be thrown by that method, instead of returning null.

    I'd put the null check and throw a NPE or other custom ProblemWithSynthesizerException of your own (Sun, for some reason, conceived this Exception as a JVM-ish exception, not meant to be used by the programmer. This is what it says in some certification tutorials and books. However, I don't buy that, and some often I throw my own NPEs in my libraries).

提交回复
热议问题