Regex or Exception Handling?

前端 未结 7 1902
暗喜
暗喜 2021-01-13 06:22

Which one of the following is a better practice to check if a string is float?

try{
 Double.parseDouble(strVal);
}catch(NumberFormatException e){
 //My Logic         


        
相关标签:
7条回答
  • 2021-01-13 06:47
    1. Performance: Exceptions are slow, and so is exception-based logic, so second would be faster.
    2. Maintenance / Reliability: The first one is crystal clear and will stay updated with updates to the Java Framework.

    That being said, I would personally prefer the first. Performance is something you want to consider as a whole in your architecture, your data structure design, etc. not line by line. Measure for performance and optimize what is actually slow, not what you think might be slow.

    0 讨论(0)
提交回复
热议问题