Java – when calling integer Should you check whether a string is empty before parseInt?

In a loop, thousands of items are converted from string to int, calling integer Should you check whether the string is empty before parseInt, or should you directly rely on numberformatexception to move to the next?

Solution

No,

In any case, you must catch numberformatexception, so adding an additional check will only add more code, which readers must pass to get functionality

(I suppose you want to check whether it is invalid in any case, not just whether it is empty. If you just want to check whether it is empty rather than whether it is usually invalid, obviously you just need to use isempty() and don't catch anything at all!)

Yes, exceptions should not normally be used for control flow - but catching numberformatexception to check whether a string is valid, int is a fairly well understood exception to this rule

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>