Which is better/more efficient: check for bad values or catch Exceptions in Java

前端 未结 11 1649
旧巷少年郎
旧巷少年郎 2020-12-15 08:11

Which is more efficient in Java: to check for bad values to prevent exceptions or let the exceptions happen and catch them?

Here are two blocks of sample code to ill

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 08:38

    In my opinion you should have try/catch blocks around anything that could potentially throw exceptions, if only to have a safe running system. You have finer control of error responses if you check for possible data errors fist. So I suggest doing both.

提交回复
热议问题