Get detail messages of chained exceptions Java

后端 未结 5 579
面向向阳花
面向向阳花 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 16:52

    You can just add the previous exception message on each exception

    This is an example :

    public static void main(String[] args) {
    
        try {
            try {
                try {
                    try {
                        throw new Exception();
                        // Some error here
                    } catch (Exception e) {
                        throw new Exception("FIRST EXCEPTION", e);
                    }
                } catch (Exception e) {
                    Exception e2 = new Exception("SECOND EXCEPTION + " + e.getMessage());
                    throw e2;
                }
            } catch (Exception e) {
                Exception e3 = new Exception("THIRD EXCEPTION + " + e.getMessage());
                throw e3;
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    

    The result is : java.lang.Exception: THIRD EXCEPTION + SECOND EXCEPTION + FIRST EXCEPTION

提交回复
热议问题