下面列出了org.apache.commons.lang.exception.ExceptionUtils#getCause ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 为解决嵌套Exception的异常输出问题,针对无message的递归获取cause的节点,保证拿到正确的cause异常 <br/>
* 如果自己有设置过message,以当前异常的message为准
*/
public String getMessage() {
if (super.getMessage() != null) {
return super.getMessage();
} else {
Throwable ca = this;
do {
Throwable c = ExceptionUtils.getCause(ca);
if (c != null) {
ca = c;
} else {
break;
}
} while (ca.getMessage() == null);
return ca.getMessage();
}
}
/**
* 为解决嵌套Exception的异常输出问题,针对无message的递归获取cause的节点,保证拿到正确的cause异常 <br/>
* 如果自己有设置过message,以当前异常的message为准
*/
@Override
public String getMessage() {
if (super.getMessage() != null) {
return super.getMessage();
} else if (cause != null) {
Throwable ca = cause;
while (this.getClass().isInstance(ca) && ca.getMessage() == null) {
Throwable c = ExceptionUtils.getCause(ca);
if (c != null) {
ca = c;
} else {
break;
}
}
if (ca != null) {
return ca.getMessage();
} else {
return null;
}
} else {
return null;
}
}
/**
* <p>Returns the list of <code>Throwable</code> objects in the
* exception chain.</p>
*
* <p>A throwable without cause will return a list containing
* one element - the input throwable.
* A throwable with one cause will return a list containing
* two elements. - the input throwable and the cause throwable.
* A <code>null</code> throwable will return a list of size zero.</p>
*
* <p>This method handles recursive cause structures that might
* otherwise cause infinite loops. The cause chain is processed until
* the end is reached, or until the next item in the chain is already
* in the result set.</p>
*
* @param throwable the throwable to inspect, may be null
* @return the list of throwables, never null
* @since Commons Lang 2.2
*/
public static List getThrowableList(Throwable throwable) {
List list = new ArrayList();
while (throwable != null && list.contains(throwable) == false) {
list.add(throwable);
throwable = ExceptionUtils.getCause(throwable);
}
return list;
}
/**
* <p>Returns the list of <code>Throwable</code> objects in the
* exception chain.</p>
*
* <p>A throwable without cause will return a list containing
* one element - the input throwable.
* A throwable with one cause will return a list containing
* two elements. - the input throwable and the cause throwable.
* A <code>null</code> throwable will return a list of size zero.</p>
*
* <p>This method handles recursive cause structures that might
* otherwise cause infinite loops. The cause chain is processed until
* the end is reached, or until the next item in the chain is already
* in the result set.</p>
*
* @param throwable the throwable to inspect, may be null
* @return the list of throwables, never null
* @since Commons Lang 2.2
*/
public static List getThrowableList(Throwable throwable) {
List list = new ArrayList();
while (throwable != null && list.contains(throwable) == false) {
list.add(throwable);
throwable = ExceptionUtils.getCause(throwable);
}
return list;
}
/**
* <p>Returns the list of <code>Throwable</code> objects in the
* exception chain.</p>
*
* <p>A throwable without cause will return a list containing
* one element - the input throwable.
* A throwable with one cause will return a list containing
* two elements. - the input throwable and the cause throwable.
* A <code>null</code> throwable will return a list of size zero.</p>
*
* <p>This method handles recursive cause structures that might
* otherwise cause infinite loops. The cause chain is processed until
* the end is reached, or until the next item in the chain is already
* in the result set.</p>
*
* @param throwable the throwable to inspect, may be null
* @return the list of throwables, never null
* @since Commons Lang 2.2
*/
public static List getThrowableList(Throwable throwable) {
List list = new ArrayList();
while (throwable != null && list.contains(throwable) == false) {
list.add(throwable);
throwable = ExceptionUtils.getCause(throwable);
}
return list;
}