Gostaria de saber como eu poderia explorar uma exceção “final”, contendo uma mensagem detalhada com todas as mensagens detalhadas de várias exceções encadeadas.
Por exemplo, suponha um código como este:
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); }
Eu não estou interessado no stackTrace
completo, mas apenas nas mensagens que escrevi. Quer dizer, eu gostaria de ter um resultado assim:
java.lang.Exception: THIRD EXCEPTION + SECOND EXCEPTION + FIRST EXCEPTION
Eu acho que o que você precisa é:
public static List getExceptionMessageChain(Throwable throwable) { List result = new ArrayList (); while (throwable != null) { result.add(throwable.getMessage()); throwable = throwable.getCause(); } return result; //["THIRD EXCEPTION", "SECOND EXCEPTION", "FIRST EXCEPTION"] }
você pode usar melhor desta forma, mesclar a message()
da Exception
anterior com a message()
da nova Exception
você está throw
:
} catch (Exception e) { throw new Exception("FIRST EXCEPTION" + e.getMessage(), e); }
Percorra a causa da exceção e anexe a mensagem em cada exceção.
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); }
Quais impressões
TERCEIRA EXCEÇÃO SEGUNDA EXCEÇÃO PRIMEIRA EXCEÇÃO Mensagem
Você pode apenas adicionar a mensagem de exceção anterior em cada exceção
Isto é um exemplo :
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); } }
O resultado é: java.lang.Exception: TERCEIRA EXCEÇÃO + SEGUNDA EXCEÇÃO + PRIMEIRA EXCEÇÃO
Eu salvei todos os atributos em um object de class com o seguinte exemplo:
public List getMessageList(Throwable throwable) { List errorMessageList = new ArrayList (); 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; }