public class TestChainException {
public static void main(String[] args) throws Throwable {
catchException();
}
public static void catchException() throws Throwable{
try {
throwException();
} catch (Exception e) {
System.out.println("------------------");
throw e;
// throw e.fillInStackTrace();
}
}
public static void throwException() throws Exception{
System.out.println("good");
throw new Exception("this is a exception");
}
}
输出结果为:
good
------------------
Exception in thread "main" java.lang.Exception: this is a exception
at TestChainException.throwException(TestChainException.java:20)
at TestChainException.catchException(TestChainException.java:11)
at TestChainException.main(TestChainException.java:5)
去掉注释:并注释:throw e;
good
------------------
Exception in thread "main" java.lang.Exception: this is a exception
at TestChainException.catchException(TestChainException.java:15)
at TestChainException.main(TestChainException.java:5)
区别两个结果会发现异常抛出信息的差别,使用throw e.fillInStackTrace();后异常抛出信息的抛出点不再有at TestChainException.throwException(TestChainException.java:20)