为什么会出现此NumberFormatException? [英] Why this NumberFormatException?

查看:80
本文介绍了为什么会出现此NumberFormatException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此堆栈跟踪(的一部分)

I have this stack trace (part of)

Servlet.service() for servlet action threw exception
java.lang.NumberFormatException: For input string: "37648"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
 at java.lang.Long.parseLong(Long.java:403)
 at java.lang.Long.valueOf(Long.java:482)
 at java.lang.Long.decode(Long.java:593)

在我的一个日志文件中我不知道什么是真正的输入字符串.但是用户已经进行了相同的堆栈跟踪.

in one of my logfile I don't know what was real input string. But the user had made happen the same stack trace.

这样的堆栈跟踪如何发生?

How such a stacktrace can happen?

推荐答案

可能是因为它们的输入中有前导零.

Probably because they have a leading zero in their input.

运行正常:

public class DecodeLong
{
    public static final void main(String[] params)
    {
        long    l;

        l = Long.decode("37648");
        System.out.println("l = " + l);
    }
}

但是,如果您更改此设置:

But if you change this:

l = Long.decode("37648");

对此:

l = Long.decode("037648");

...它变成无效的八进制,并且 Long.parseLong 的异常不包括前导零:

...it becomes invalid octal, and the exception from Long.parseLong doesn't include the leading zero:

Exception in thread "main" java.lang.NumberFormatException: For input string: "37648"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Long.parseLong(Unknown Source)
        at java.lang.Long.valueOf(Unknown Source)
        at java.lang.Long.decode(Unknown Source)
        at DecodeLong.main(DecodeLong.java:24)

它不包含它,因为 decode 调用 parseLong 不带零,但基数设置为8.

It doesn't include it because decode calls parseLong without the zero, but with the base set to 8.

谈论晦涩难懂的话题.:-)因此,如果您通过显示 actual 输入来更新程序以处理异常,则可能会发现这些内容.

Talk about obscure. :-) So if you update your program to handle the exception by showing the actual input, you'll probably find it's something along those lines.

这篇关于为什么会出现此NumberFormatException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆