Is it OK to try/catch something just to check if an exception was thrown or not?

前端 未结 10 635
Happy的楠姐
Happy的楠姐 2020-12-28 13:09

Is it a good way to try something useless just to see if a particular exception is thrown by this code ?
I want to do something when the exception is thrown, and nothing

10条回答
  •  长发绾君心
    2020-12-28 13:41

    You have to consider that the construction of the Exception object is expensive in terms of time and resources for the JVM, because it has to construct the strack trace.

    So what you propose is an easy but resource-consuming way to solve the problem.

    So wether this solution is acceptable or not depends on the use you are going to give to this function, and your efficiency requirements.

提交回复
热议问题