blob: aaccc383e6f314227337109e344d40dc676817e0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2000, 2015 Oracle and/or its affiliates. All rights reserved.
*
*/
package com.sleepycat.util;
/**
* Unwraps nested exceptions by calling the {@link
* ExceptionWrapper#getCause()} method for exceptions that implement the
* {@link ExceptionWrapper} interface. Does not currently support the Java 1.4
* <code>Throwable.getCause()</code> method.
*
* @author Mark Hayes
*/
public class ExceptionUnwrapper {
/**
* Unwraps an Exception and returns the underlying Exception, or throws an
* Error if the underlying Throwable is an Error.
*
* @param e is the Exception to unwrap.
*
* @return the underlying Exception.
*
* @throws Error if the underlying Throwable is an Error.
*
* @throws IllegalArgumentException if the underlying Throwable is not an
* Exception or an Error.
*/
public static Exception unwrap(Exception e) {
Throwable t = unwrapAny(e);
if (t instanceof Exception) {
return (Exception) t;
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new IllegalArgumentException("Not Exception or Error: " + t);
}
}
/**
* Unwraps an Exception and returns the underlying Throwable.
*
* @param e is the Exception to unwrap.
*
* @return the underlying Throwable.
*/
public static Throwable unwrapAny(Throwable e) {
while (true) {
if (e instanceof ExceptionWrapper) {
Throwable e2 = ((ExceptionWrapper) e).getCause();
if (e2 == null) {
return e;
} else {
e = e2;
}
} else {
return e;
}
}
}
}
|