Path: blob/master/test/jdk/java/lang/Throwable/ChainedExceptions.java
41152 views
/*1* @test /nodynamiccopyright/2* @bug 4209652 43633183* @summary Basic test for chained exceptions & Exception.getStackTrace().4* @author Josh Bloch5*/67public class ChainedExceptions {8public static void main(String args[]) {9try {10a();11} catch(HighLevelException e) {12StackTraceElement[] highTrace = e.getStackTrace();13int depthTrim = highTrace.length - 2;1415check(e, highTrace[0], "a", 48);16check(e, highTrace[1], "main", 11);1718Throwable mid = e.getCause();19StackTraceElement[] midTrace = mid.getStackTrace();20if (midTrace.length - depthTrim != 4)21throw new RuntimeException("Mid depth");22check(mid, midTrace[0], "c", 58);23check(mid, midTrace[1], "b", 52);24check(mid, midTrace[2], "a", 46);25check(mid, midTrace[3], "main", 11);2627Throwable low = mid.getCause();28StackTraceElement[] lowTrace = low.getStackTrace();29if (lowTrace.length - depthTrim != 6)30throw new RuntimeException("Low depth");31check(low, lowTrace[0], "e", 65);32check(low, lowTrace[1], "d", 62);33check(low, lowTrace[2], "c", 56);34check(low, lowTrace[3], "b", 52);35check(low, lowTrace[4], "a", 46);36check(low, lowTrace[5], "main", 11);3738if (low.getCause() != null)39throw new RuntimeException("Low cause != null");40}41}4243static void a() throws HighLevelException {44try {45b();46} catch(MidLevelException e) {47throw new HighLevelException(e);48}49}50static void b() throws MidLevelException {51c();52}53static void c() throws MidLevelException {54try {55d();56} catch(LowLevelException e) {57throw new MidLevelException(e);58}59}60static void d() throws LowLevelException {61e();62}63static void e() throws LowLevelException {64throw new LowLevelException();65}6667private static final String OUR_CLASS = ChainedExceptions.class.getName();68private static final String OUR_FILE_NAME = "ChainedExceptions.java";6970private static void check(Throwable t, StackTraceElement e, String methodName, int n) {71if (!e.getClassName().equals(OUR_CLASS))72throw new RuntimeException("Class: " + e, t);73if (!e.getMethodName().equals(methodName))74throw new RuntimeException("Method name: " + e, t);75if (!e.getFileName().equals(OUR_FILE_NAME))76throw new RuntimeException("File name: " + e, t);77if (e.getLineNumber() != n)78throw new RuntimeException("Line number: " + e, t);79}80}8182class HighLevelException extends Exception {83HighLevelException(Throwable cause) { super(cause); }84}8586class MidLevelException extends Exception {87MidLevelException(Throwable cause) { super(cause); }88}8990class LowLevelException extends Exception {91}929394