jdk/test/java/lang/Throwable/ChainedExceptions.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 9513 1079ae7ada52
permissions -rw-r--r--
Initial load
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
            check(highTrace[0], "a",    48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
            check(highTrace[1], "main", 11);
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");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
            check(midTrace[0], "c",    58);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
            check(midTrace[1], "b",    52);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
            check(midTrace[2], "a",    46);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
            check(midTrace[3], "main", 11);
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");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
            check(lowTrace[0], "e",    65);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
            check(lowTrace[1], "d",    62);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
            check(lowTrace[2], "c",    56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
            check(lowTrace[3], "b",    52);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
            check(lowTrace[4], "a",    46);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
            check(lowTrace[5], "main", 11);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static void check(StackTraceElement e, String methodName, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (!e.getClassName().equals(OUR_CLASS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new RuntimeException("Class: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (!e.getMethodName().equals(methodName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new RuntimeException("Method name: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (!e.getFileName().equals(OUR_FILE_NAME))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new RuntimeException("File name: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (e.getLineNumber() != n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new RuntimeException("Line number: " + e);
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
}