jjzjj

countStr

全部标签

java - 哪个更好 - Integer.parseInt() 并捕获异常或在转换前使用模式验证字符串?

这个问题在这里已经有了答案:Whichisbetter/moreefficient:checkforbadvaluesorcatchExceptionsinJava(11个答案)关闭9年前。我见过两种在Java中检查变量是否为有效整数的样式。一种方法是执行Integer.parseInt并捕获任何由此产生的异常。另一种是使用模式。以下哪种方法更好?StringcountStr;intcount;try{count=Integer.parseInt(countStr);}catch(Exceptione){//returnasthevariableisnotaproperinteger.