zoukankan      html  css  js  c++  java
  • ThrowableUtil

    public class ThrowableUtil
    {
        public static Throwable getDeepestCause(final Throwable throwable) {
            int count;
            Throwable cause;
            for (count = 5, cause = throwable; cause.getCause() != null && count > 0; cause = cause.getCause(), --count) {}
            return cause;
        }
        
        public static String getStackPrint(final Throwable throwable) {
            if (throwable == null) {
                return null;
            }
            final Throwable cause = getDeepestCause(throwable);
            final StringBuilder sb = new StringBuilder(cause.getClass().getName());
            sb.append(":");
            sb.append(cause.getMessage());
            sb.append("
    ");
            final StackTraceElement[] stackArray = cause.getStackTrace();
            for (int i = 0; i < stackArray.length; ++i) {
                final StackTraceElement element = stackArray[i];
                sb.append("	");
                sb.append(element.toString());
                sb.append("
    ");
            }
            return sb.toString();
        }
        
        public static void main(final String[] args) {
            try {
                try {
                    throw new RuntimeException("throw it ....");
                }
                catch (Throwable e) {
                    final String msg = getStackPrint(e);
                    throw e;
                }
            }
            catch (Throwable e) {
                getStackPrint(e);
            }
        }
    }
  • 相关阅读:
    Java加密作业
    作业
    思考动手
    方法作业
    课堂2数字输出
    字符型转整形
    课堂验证作业
    Eclipse @override报错解决
    用注解配置动态代理
    动态代理模式
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/7097379.html
Copyright © 2011-2022 走看看