关于java异常e.toString是否存在安全泄露的问题

1.首先我们依旧看下e.toString()的API官方文档

e.toString 的是否存在安全泄露的问题

String java.lang.Throwable.toString()

Returns a short description of this throwable. The result is the concatenation of:

  • the name of the class of this object
  • ": " (a colon and a space)
  • the result of invoking this object's getLocalizedMessage method

If getLocalizedMessage returns null, then just the class name is returned.

Overrides: toString() in Object

Returns:

a string representation of this throwable.

2.看不懂没关系,看下面的实例:


public class TestString
{

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub

        try
        {

            String password = "123";
            System.out.println(password.charAt(20));

        }
        catch (Exception e)
        {
            System.out.println(e.toString());

        }

        F t = new F();

        try
        {
            t.paly();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println(e.toString());
        }

        try
        {
            t.play();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println(e.toString());
        }

    }

}

class F
{
    void play() throws Exception
    {

        throw new Exception();

    }

    void paly()
    {
        int t = 0;

        int y = 7 / 0;
    }

}

看结果:

 分析结果可知:

1.如果是类本身时候的异常,会打印出引起异常的JDK类名和原因;

2.如果是类抛出的异常,同上,会打印出引起异常的类名和原因;

3.如果是运行时候的异常,会打印出引起异常的类名;

 

也就是说:异常不会在一般的情况下不会打印异常发生的类;也不会打印异常本身。异常打印的是引发异常的JDK源码的基本信息

 

3.回到API官方指南

1.说的是e.toString 会打印出异常最基本的信息包括引发异常的类名,如果你没有自己向本地异常注入太多的信息,他是不会打印多余的信息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值