获取链式异常Java的详细消息

时间:2013-04-13 11:24:25

标签: java exception exception-handling chaining

我想知道我如何能够选择“最终”Exception,其中包含详细信息,其中包含许多链接异常的所有详细信息。

例如假设这样的代码:

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

我对完整的stackTrace不感兴趣,但只对我写的信息感兴趣。我的意思是,我希望得到这样的结果:

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

6 个答案:

答案 0 :(得分:26)

我认为你需要的是:

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

答案 1 :(得分:1)

您可以通过这种方式更好地使用它,将之前message()的{​​{1}}与您Exception的新message()的{​​{1}}合并:

Exception

答案 2 :(得分:1)

循环遍历异常原因并在每个异常中附加消息。

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

打印

  

第三个例外第二个例外情况第一个例外消息

答案 3 :(得分:1)

您只需在每个例外添加上一个异常消息

这是一个例子:

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

结果是:java.lang.Exception:THIRD EXCEPTION + SECOND EXCEPTION + FIRST EXCEPTION

答案 4 :(得分:1)

这是一个将链式异常转换为字符串的好工具:

public final class ThrowableUtil {

    private ThrowableUtil() {}

    public static String chainedString(@NonNull Throwable throwable) {
        StringBuilder SB = new StringBuilder(throwable.toString());
        while((throwable = throwable.getCause()) != null)
            SB.append("\ncaused by ").append(throwable);
        return SB.toString();
    }

    public static String chainedString(@NonNull String msg, @NonNull Throwable throwable) {
        StringBuilder SB = new StringBuilder(msg);
        do {
            SB.append("\ncaused by ").append(throwable);
        } while((throwable = throwable.getCause()) != null);
        return SB.toString();
    }

}

示例输出:

ThrowableUtil.chainedString(e);

生产

java.io.IOException: Failed to create required video encoder
caused by java.lang.RuntimeException: Invalid mime type

另一个示例输出:

ThrowableUtil.chainedString("Writing of media file failed", e);

生产

Writing of media file failed
caused by java.io.IOException: Failed to create required video encoder
caused by java.lang.RuntimeException: Invalid mime type

答案 5 :(得分:0)

我使用以下示例保存了类对象中的所有属性:

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