为什么 Double.parseDouble(null) 和 Integer.parseInt(null) 会抛出不同的异常?
这是历史意外还是故意的?文档清楚地说明了 Double.parseDouble(...)
的两种异常(exception)情况。一个用于 Integer.parseInt()
,但似乎不一致:
Integer.parseInt(null); // throws java.lang.NumberFormatException: null
然而
Double.parseDouble(null); // throws java.lang.NullPointerException
最佳答案
It is reasonable to expect the same exceptions to be thrown for null; however, these api's are very old and may not be able to be changed at this point.
还有:
Since the exception behavior is long-standing and specified in the JavaDoc, it is impractical to change either method's behavior at this time. Closing as will not fix.
取自:Bug Report: Integer.parseInt() and Double.parseDouble() throw different exceptions on null.
正如其他人所说:它可能由不同的作者制作。
关于java - 为什么 Double.parseDouble(null) 和 Integer.parseInt(null) 会抛出不同的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16324831/