If catching null pointer exception is not a good practice, is catching exception a good one?

后端 未结 6 1709
故里飘歌
故里飘歌 2020-12-03 01:56

I have heard that catching NullPointerException is a bad practice, and i think it is sensibly so. Letting the NullPointerException to propagate to

6条回答
  •  一向
    一向 (楼主)
    2020-12-03 02:14

    The main rule about catching exception is that you have to know why you are doing this. Exception class is caught in cases when programmer wants to do generic error processing and he does not really care what exactly happened, the main thing is just something went wrong. In that case he may decide to rollback transaction or do some cleanup. If you are catching specific exception try to apply the same rule. I you know exactly why you are doing that, then it's to do that. But it's very rare case when someone would want to do something really special in case of NPE.

提交回复
热议问题