创建短号时的NumberFormatException

时间:2018-05-09 07:21:33

标签: java exception numberformatexception short

我正在处理包装类在以下代码中面对NumberFormatException时请告诉我为什么在此运行时异常之前我们没有遇到编译时错误,为什么没有编译时错误只是运行时NumberFormat异常。

COMError: (-2146822496, None, ('The file appears to be corrupted.', 
'Microsoft Word', 'wdmain11.chm', 25272, None))

异常

  

线程“main”中的异常java.lang.NumberFormatException:值超出范围。值:“32770”基数:10

3 个答案:

答案 0 :(得分:2)

请参阅tutorial

Short的范围从java中的-32,76832,76732770不是有效的short

答案 1 :(得分:0)

只有以下情况才会出现例外情况:

An exception of type NumberFormatException is thrown if any of the following situations occurs:

    The first argument is null or is a string of length zero.
    The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
    Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') or plus sign '+' ('\u002B') provided that the string is longer than length 1.
    The value represented by the string is not a value of type short.

答案 2 :(得分:0)

如前所述范围从-32768到32767 。您将“32770”作为值传递给超出范围的构造函数。

您没有收到任何编译时错误,因为您正在将有效类型的参数传递给构造函数。 该值在实际执行期间验证,这在运行时发生,因此NumberFormatException而不是任何编译错误