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

前端 未结 10 652
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:38

    Generally, this practice should be avoided. But since there is no utility method isValidBigDecimal(..), that's the way to go.

    As Peter Tillemans noted in the comments, place this code in a utility method called isValidBigDecimal(..). Thus your code will be agnostic of the way of determining the validity, and you can even later switch to another method.

    Boris Pavlović suggested an option to check that using a 3rd party library (commons-lang). There's one more useful method there, which I use whenever I need to verify numbers - NumberUtils.isNumber(..)

提交回复
热议问题