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

28

我想知道如何抛出一个"final"异常,其中包含一条详细的消息和多个链接异常的所有详细消息。

例如,假设有如下代码:

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
7个回答

42

我认为你需要的是:

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

非常好的解决方案,谢谢!其他建议也很好,但这样我可以将代码集中在一个单一的方法中,并且我只需要修改抛出final Exception的方法... - MikO

3

你可以更好地使用这种方法,将前一个Exceptionmessage()与你正在throw的新Exceptionmessage()合并:

      } catch (Exception e) {
          throw new Exception("FIRST EXCEPTION" + e.getMessage(), e);
      }

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

想知道是否已经在Apache或Google的“异常工具”中实现了,但是找不到:( - undefined

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

这会打印出:

第三个异常 第二个异常 第一个异常 信息


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。

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

0
也许更简单
try {
   // code that throws exception
} catch(Throwable e ) {
    var messages = new ArrayList<String>();
    
    do {
        messages.add(e.getMessage());
        e = e.getCause();
    } while( e!= null ); 
    
    var message = String.join(" -> ", messages);    
    System.out.println(message);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接