为什么toString方法显示错误?

时间:2013-07-18 16:41:38

标签: java exception-handling string-conversion

这是我的代码。我已经覆盖了toString()方法。因此,当正在打印对象e时,必须调用重写的方法。但我没有看到被调用的方法。

public static void main(String[] args) {

        try
        {
            String name="foo";
            int a =Integer.parseInt(name);
        }catch (NumberFormatException e)
        {
            System.out.println(e);
        }

    }
    public String toString()
    {
        return "err";
    }

}

1 个答案:

答案 0 :(得分:0)

您已对所做的任何课程重写toString。但是编写System.out.println(e);会隐式调用toString类上的NumberFormatException方法,而不是类。

为什么你试图覆盖toString上的NumberFormatException呢? System.out.println(e.getMessage());甚至e.printStackTrace();有什么问题?

相关问题