Java 获取链接异常的详细消息
我想知道如何抛出一个“final”Java 获取链接异常的详细消息,java,exception,chaining,Java,Exception,Chaining,我想知道如何抛出一个“final”异常,其中包含一条详细消息,其中包含许多链接异常的所有详细消息 例如,假设代码如下所示: try { try { try { try { //Some error here } catch (Exception e) { throw new Exception("FIRST EXCEPTION", e); } } catch (Exception e) {
异常
,其中包含一条详细消息,其中包含许多链接异常的所有详细消息
例如,假设代码如下所示:
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);
}
java.lang.Exception: THIRD EXCEPTION + SECOND EXCEPTION + FIRST EXCEPTION
我对完整的stackTrace
不感兴趣,只对消息感兴趣,我写道。我的意思是,我希望得到这样的结果:
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);
}
java.lang.Exception: THIRD EXCEPTION + SECOND EXCEPTION + FIRST EXCEPTION
您可以通过这种方式更好地使用它,将以前的
异常的消息()
与新的异常的消息()
合并,您将抛出:
} catch (Exception e) {
throw new Exception("FIRST EXCEPTION" + e.getMessage(), e);
}
循环检查异常原因,并在每个异常中附加消息
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);
}
哪张照片
第三个异常第二个异常第一个异常消息
我想你需要的是:
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"]
}
publicstaticlist getExceptionMessageChain(Throwable-Throwable){
列表结果=新建ArrayList();
while(可丢弃!=null){
add(throwable.getMessage());
throwable=throwable.getCause();
}
返回结果;//[“第三个异常”、“第二个异常”、“第一个异常”]
}
您只需在每个异常上添加上一条异常消息即可
这是一个例子:
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我用以下示例保存了class对象中的所有属性:
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;
}
public List getMessageList(Throwable-Throwable){
List errorMessageList=新建ArrayList();
while(可丢弃!=null){
ErrorMessage=新的ErrorMessage();
message.set_message(throwable.getMessage());
message.set_行(throwable.getStackTrace()[0].getLineNumber());
message.set_methodName(throwable.getStackTrace()[0].getMethodName());
message.set_文件名(throwable.getStackTrace()[0].getFileName());
message.set_className(throwable.getStackTrace()[0].getClassName());
errorMessageList.add(消息);
throwable=throwable.getCause();
}
返回错误消息列表;
}
这里有一个很好的实用程序,用于将链式异常转换为字符串:
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);
ThrowableUtil.chainedString("Writing of media file failed", e);
产生
java.io.IOException: Failed to create required video encoder
caused by java.lang.RuntimeException: Invalid mime type
Writing of media file failed
caused by java.io.IOException: Failed to create required video encoder
caused by java.lang.RuntimeException: Invalid mime type
另一个示例输出:
ThrowableUtil.chainedString(e);
ThrowableUtil.chainedString("Writing of media file failed", e);
产生
java.io.IOException: Failed to create required video encoder
caused by java.lang.RuntimeException: Invalid mime type
Writing of media file failed
caused by java.io.IOException: Failed to create required video encoder
caused by java.lang.RuntimeException: Invalid mime type
很好的解决方案,thanx!其他的建议也很好,但是这样我就可以将代码集中在一个方法中,并且我只需要修改我抛出最后一个异常的方法
。。。