com.baidu.aip.util.ExceptionUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk Show documentation
Show all versions of java-sdk Show documentation
The AIP SDK for Java provides Java APIs for all of AI APIs.
The newest version!
package com.baidu.aip.util;
public class ExceptionUtil {
// 打印异常堆栈信息
public static String getStackTraceString(Throwable ex) {
StackTraceElement[] traceElements = ex.getStackTrace();
StringBuilder traceBuilder = new StringBuilder();
if (traceElements != null && traceElements.length > 0) {
for (StackTraceElement traceElement : traceElements) {
traceBuilder.append(traceElement.toString());
traceBuilder.append("\n");
}
}
return traceBuilder.toString();
}
// 构造异常堆栈信息
public static String buildErrorMessage(Exception ex) {
String result;
String stackTrace = getStackTraceString(ex);
String exceptionType = ex.toString();
String exceptionMessage = ex.getMessage();
result = String.format("%s : %s \r\n %s", exceptionType, exceptionMessage, stackTrace);
return result;
}
}