惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - 有容乃大

GitHub相关 2022又要重新找工作开始新的历程 java之面向对象详解(转) java关于for循环的效率优化 JAVA线程池ThreadPoolExecutor的分析和使用(新手踩坑和推荐方案) JAVA常量池 Java String的intern()注意事项(分JDK1.6及JDK1.7) JAVA的类加载过程 使用RabbitMQ实现延迟任务 JAVA三元运算符空指针引用的坑 Java中static块、构造块、构造函数的执行顺序 Ansj中文分词 关于HashMap、HashSet和ArrayList集合对象容量初始值设置及扩容演示 JVM的内存区域划分 深入理解Java String类(综合) 理解JAVA的IO 理解Java注解类型 JAVA中Integer的==和equals注意 JVM原理摘要
将异常对象转为字符串
有容乃大 · 2020-01-03 · via 博客园 - 有容乃大
/**
     * 将异常对象转为字符串。
     *
     * @param ex 异常信息
     * @return 字符串
     */
    public static String exceptionToString(Throwable ex) {
        //获取指定Throwable对象中最底层的Throwable
        Throwable lowerThrowable = getLowerThrowable(ex);

        //获取异常堆栈信息。
        StringBuilder sb = new StringBuilder(81920);
        exceptionToString(ex, lowerThrowable, sb);

        return sb.toString();
    }

    /**
     * 将异常对象转为字符串。
     *
     * @param ex 异常信息
     * @return 字符串
     */
    private static void exceptionToString(Throwable ex, Throwable lowerThrowable, StringBuilder sb) {
        sb.append(ex.toString());
        sb.append(SystemCharUtils.getNewLine());

        if (ex.equals(lowerThrowable)) {
            for (StackTraceElement el : ex.getStackTrace()) {
                sb.append(el.toString());
                sb.append(SystemCharUtils.getNewLine());
            }
        }

        if (null != ex.getCause()) {
            exceptionToString(ex.getCause(), lowerThrowable, sb);
        }
    }

    /**
     * 获取指定Throwable对象中最底层的Throwable。
     *
     * @param e Throwable对象
     * @return 最底层的Throwable
     */
    public static Throwable getLowerThrowable(Throwable e) {
        if (null == e.getCause()) {
            return e;
        }

        return getLowerThrowable(e.getCause());
    }