Get detail messages of chained exceptions Java

后端 未结 5 578
面向向阳花
面向向阳花 2020-12-28 16:29

I\'d like to know how I could I thorw a \"final\" Exception, containing a detail message with all the detail messages of a number of chained exceptions.

5条回答
  •  滥情空心
    2020-12-28 17:07

    I think what you need is:

    public static List getExceptionMessageChain(Throwable throwable) {
        List result = new ArrayList();
        while (throwable != null) {
            result.add(throwable.getMessage());
            throwable = throwable.getCause();
        }
        return result; //["THIRD EXCEPTION", "SECOND EXCEPTION", "FIRST EXCEPTION"]
    }
    

提交回复
热议问题