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

com.base4j.util.exceptions.ExceptionUtil Maven / Gradle / Ivy

The newest version!
package com.base4j.util.exceptions;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

import com.base4j.util.StrUtil;

/**
 * 异常工具类
 *
 * @author Looly
 */
public class ExceptionUtil {

    /**
     * 获得完整消息,包括异常名
     *
     * @param e 异常
     * @return 完整消息
     */
    public static String getMessage(Throwable e) {
        return StrUtil.format("{}: {}", e.getClass().getSimpleName(), e.getMessage());
    }

    /**
     * 剥离反射引发的InvocationTargetException、UndeclaredThrowableException中间异常,返回业务本身的异常
     *
     * @param wrapped 包装的异常
     * @return 剥离后的异常
     */
    public static Throwable unwrap(Throwable wrapped) {
        Throwable unwrapped = wrapped;
        while (true) {
            if (unwrapped instanceof InvocationTargetException) {
                unwrapped = ((InvocationTargetException) unwrapped).getTargetException();
            } else if (unwrapped instanceof UndeclaredThrowableException) {
                unwrapped = ((UndeclaredThrowableException) unwrapped).getUndeclaredThrowable();
            } else {
                return unwrapped;
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy