Get detail messages of chained exceptions Java

放肆的年华 提交于 2019-12-18 11:48:49

问题


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.

For example suppose a code like this:

try {
  try {
    try {
      try {
        //Some error here
      } 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 allMessages = //all the messages
  throw new Exception(allMessages, e);
}

I'm not interested in the full stackTrace, but only in the messages I wrote. I mean, I'd like to have a result like this:

java.lang.Exception: THIRD EXCEPTION + SECOND EXCEPTION + FIRST EXCEPTION

回答1:


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"]
}



回答2:


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);
      }



回答3:


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




回答4:


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




回答5:


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; 
    }


来源:https://stackoverflow.com/questions/15987258/get-detail-messages-of-chained-exceptions-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!