2013-04-13 20 views
15

Vorrei sapere come è possibile ottenere un finaleException, contenente un messaggio di dettaglio con tutti i messaggi di dettaglio di un numero di eccezioni concatenate.Ottenere messaggi di dettaglio delle eccezioni concatenate Java

Per esempio supponiamo un codice come questo:

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

non mi interessa nel pieno stackTrace, ma solo nei messaggi che ho scritto. Voglio dire, mi piacerebbe avere un risultato come questo:

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

risposta

22

Penso che quello che vi serve è:

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

Ottima soluzione, Grazie! Anche altri suggerimenti erano buoni, ma in questo modo ho il codice centralizzato in un unico metodo, e devo solo modificare i metodi da dove lancio la * final * 'Exception' ... – MikO

1

si può meglio usarlo in questo modo, unire il message() della precedente Exception con l'message() di nuova Exception sei throw ing:

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

Esegue ciclicamente la causa dell'eccezione e aggiunge il messaggio in ogni eccezione.

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

che stampa

ECCEZIONE TERZO seconda eccezione prima eccezione Messaggio

1

si può semplicemente aggiungere il messaggio di eccezione precedente a ciascuna eccezione

Questo è un esempio:

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

Il risultato è: java.lang.Exception: terza deroga + seconda eccezione + prima eccezione