Java: check for null or allow exception handling

前端 未结 6 2213
旧巷少年郎
旧巷少年郎 2020-12-10 04:56

I\'m wondering about the cost of using a try/exception to handle nulls compared to using an if statement to check for nulls first.

To provide more information. There

6条回答
  •  没有蜡笔的小新
    2020-12-10 05:23

    I'd recommend checking for null and not doing the calculation rather than throwing an exception.

    An exception should be "exceptional" and rare, not a way to manage flow of control.

    I'd also suggest that you establish a contract with your clients regarding input parameters. If nulls are allowed spell it out; if they're not, make it clear what should be passed, default values, and what you promise to return if a null value is passed.

提交回复
热议问题