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 会打印出异常最基本的信息包括引发异常的类名,如果你没有自己向本地异常注入太多的信息,他是不会打印多余的信息。