Get detail messages of chained exceptions Java

后端 未结 5 569
面向向阳花
面向向阳花 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:50

    I had saved all attributes in a class object with the following example:

    public List<ErrorMessage> getMessageList(Throwable throwable) {
            List<ErrorMessage> errorMessageList =  new ArrayList<ErrorMessage>();
            while (throwable != null) {
                ErrorMessage message = new ErrorMessage();
                message.set_message( throwable.getMessage());
                message.set_line(throwable.getStackTrace()[0].getLineNumber());
                message.set_methodName(throwable.getStackTrace()[0].getMethodName());
                message.set_fileName(throwable.getStackTrace()[0].getFileName() );
                message.set_className(throwable.getStackTrace()[0].getClassName());
                errorMessageList.add(message);
                throwable = throwable.getCause();
            }
            return errorMessageList; 
        }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-28 16:59

    Cycle through the exception cause and append the message in each exception.

        try
        {
            try
            {
                try
                {
                    try
                    {
                        throw new RuntimeException("Message");
                    }
                    catch (Exception e)
                    {
                        throw new Exception("FIRST EXCEPTION", e);
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("SECOND EXCEPTION", e);
                }
            }
            catch (Exception e)
            {
                throw new Exception("THIRD EXCEPTION", e);
            }
        }
        catch (Exception e)
        {
            String message = e.getMessage();
            Throwable inner = null;
            Throwable root = e;
            while ((inner = root.getCause()) != null)
            {
                message += " " + inner.getMessage();
                root = inner;
            }
            System.out.println(message);
        }
    

    Which prints

    THIRD EXCEPTION SECOND EXCEPTION FIRST EXCEPTION Message

    0 讨论(0)
  • 2020-12-28 17:04

    you can better use it this way, merge the message() of previous Exception with the message() of new Exception you are throwing:

          } catch (Exception e) {
              throw new Exception("FIRST EXCEPTION" + e.getMessage(), e);
          }
    
    0 讨论(0)
  • 2020-12-28 17:07

    I think what you need is:

    public static List<String> getExceptionMessageChain(Throwable throwable) {
        List<String> result = new ArrayList<String>();
        while (throwable != null) {
            result.add(throwable.getMessage());
            throwable = throwable.getCause();
        }
        return result; //["THIRD EXCEPTION", "SECOND EXCEPTION", "FIRST EXCEPTION"]
    }
    
    0 讨论(0)
提交回复
热议问题