Java – why string Indexof does not use exceptions, but returns – 1 when no substring is found?

Why string Indexof does not use exceptions, but returns - 1 if no substring is found?

The purpose of this problem is: when we start to customize exceptions

I believe that avoiding the need to return special error codes is the right design path

What's your opinion?

Solution

As a rule of thumb, if the purpose of a method is to check something, the lack of these things should not be an exception If the method assumes that something is true, the absence of that thing will be an exception Therefore, "file. Exists()" will not raise FileNotFoundException, but "file. Open()"

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
分享
二维码
< <上一篇
下一篇>>