All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.baidu.aip.util.ExceptionUtil Maven / Gradle / Ivy

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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy