jdk/test/java/lang/Throwable/ChainedExceptions.java
author katleman
Tue, 21 Jan 2014 18:17:07 -0800
changeset 22414 796c8cc1bc35
parent 9513 1079ae7ada52
permissions -rw-r--r--
Added tag jdk9-b01 for changeset 6428b06e538b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * @test    /nodynamiccopyright/
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * @bug     4209652 4363318
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * @summary Basic test for chained exceptions & Exception.getStackTrace().
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
public class ChainedExceptions {
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
            a();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
        } catch(HighLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
            StackTraceElement[] highTrace = e.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
            int depthTrim = highTrace.length - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
9513
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    16
            check(e, highTrace[0], "a",    48);
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    17
            check(e, highTrace[1], "main", 11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
            Throwable mid = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
            StackTraceElement[] midTrace = mid.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
            if (midTrace.length - depthTrim != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
                throw new RuntimeException("Mid depth");
9513
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    23
            check(mid, midTrace[0], "c",    58);
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    24
            check(mid, midTrace[1], "b",    52);
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    25
            check(mid, midTrace[2], "a",    46);
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    26
            check(mid, midTrace[3], "main", 11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
            Throwable low = mid.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
            StackTraceElement[] lowTrace = low.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
            if (lowTrace.length - depthTrim != 6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
                throw new RuntimeException("Low depth");
9513
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    32
            check(low, lowTrace[0], "e",    65);
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    33
            check(low, lowTrace[1], "d",    62);
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    34
            check(low, lowTrace[2], "c",    56);
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    35
            check(low, lowTrace[3], "b",    52);
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    36
            check(low, lowTrace[4], "a",    46);
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    37
            check(low, lowTrace[5], "main", 11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            if (low.getCause() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                throw new RuntimeException("Low cause != null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static void a() throws HighLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            b();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        } catch(MidLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            throw new HighLevelException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static void b() throws MidLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        c();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void c() throws MidLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            d();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } catch(LowLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new MidLevelException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static void d() throws LowLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
       e();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static void e() throws LowLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        throw new LowLevelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final String OUR_CLASS  = ChainedExceptions.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final String OUR_FILE_NAME = "ChainedExceptions.java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
9513
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    71
    private static void check(Throwable t, StackTraceElement e, String methodName, int n) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (!e.getClassName().equals(OUR_CLASS))
9513
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    73
            throw new RuntimeException("Class: " + e, t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (!e.getMethodName().equals(methodName))
9513
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    75
            throw new RuntimeException("Method name: " + e, t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (!e.getFileName().equals(OUR_FILE_NAME))
9513
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    77
            throw new RuntimeException("File name: " + e, t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (e.getLineNumber() != n)
9513
1079ae7ada52 6998871: Support making the Throwable.stackTrace field immutable
darcy
parents: 2
diff changeset
    79
            throw new RuntimeException("Line number: " + e, t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
class HighLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    HighLevelException(Throwable cause) { super(cause); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
class MidLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    MidLevelException(Throwable cause)  { super(cause); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
class LowLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}