2012-09-24 15 views
33

Ho una piccola parte di codice che viene eseguita attraverso alcune transazioni per l'elaborazione. Ogni transazione è contrassegnata da un numero di transazione, che è generato da un programma esterno e non è necessariamente sequenziato. Quando rilevo un'eccezione nel codice di elaborazione, la lancio alla classe principale e la registro per la revisione successiva. Vorrei aggiungere il numero di transazione a questa eccezione generata. È possibile farlo mantenendo la traccia dello stack corretta?Aggiungere un messaggio personalizzato all'eccezione generata mantenendo la traccia di stack in Java

Per esempio:

public static void main(String[] args) { 
    try{ 
     processMessage(); 
    }catch(Exception E){ 
     E.printStackTrace(); 
    } 

} 

private static void processMessage() throws Exception{ 
    String transNbr = ""; 
    try{ 
     transNbr = "2345"; 
     throw new Exception(); 
    }catch(Exception E){ 
     if(!transNbr.equals("")){ 
      //stack trace originates from here, not from actual exception 
      throw new Exception("transction: " + transNbr); 
     }else{ 
      //stack trace gets passed correctly but no custom message available 
      throw E; 
     } 
    } 
} 

risposta

56

Prova:

throw new Exception("transction: " + transNbr, E); 
+1

che è esattamente quello che stavo cercando. Grazie. – thedan

6

C'è un Exception costruttore che prende anche l'argomento causa: Exception(String message, Throwable t)

Si può usare per propagare la stacktrace:

try{ 
    ... 
}catch(Exception E){ 
    if(!transNbr.equals("")){ 
     throw new Exception("transction: " + transNbr, E); 
    } 
    ... 
} 
12

Le eccezioni sono solitamente immutabili: non è possibile modificare il loro messaggio dopo che sono stati creati. Cosa si può fare, però, è eccezioni a catena:

throw new TransactionProblemException(transNbr, originalException); 

L'analisi dello stack sarà simile

TransactionProblemException : transNbr 
at ... 
at ... 
caused by OriginalException ... 
at ... 
at ... 
+1

Questo è più bello, dal momento che il nome della classe di eccezioni è legato al dominio - è immediatamente riconoscibile esattamente la fonte del problema. Inoltre, gli strumenti di analisi del codice possono generare avvisi quando si lancia una nuova eccezione (...) '. – vikingsteve

0

È possibile utilizzare il messaggio di eccezione per: -

public class MyNullPointException extends NullPointerException { 

     private ExceptionCodes exceptionCodesCode; 

     public MyNullPointException(ExceptionCodes code) { 
      this.exceptionCodesCode=code; 
     } 
     @Override 
     public String getMessage() { 
      return exceptionCodesCode.getCode(); 
     } 


    public class enum ExceptionCodes { 
    COULD_NOT_SAVE_RECORD ("cityId:001(could.not.save.record)"), 
    NULL_POINT_EXCEPTION_RECORD ("cityId:002(null.point.exception.record)"), 
    COULD_NOT_DELETE_RECORD ("cityId:003(could.not.delete.record)"); 

    private String code; 

    private ExceptionCodes(String code) { 
     this.code = code; 
    } 

    public String getCode() { 
     return code; 
    } 
} 
1

è possibile utilizzare super durante l'estensione Eccezione

if (pass.length() < minPassLength) 
    throw new InvalidPassException("The password provided is too short"); 
} catch (NullPointerException e) { 
    throw new InvalidPassException("No password provided", e); 
} 


// A custom business exception 
class InvalidPassException extends Exception { 

InvalidPassException() { 

} 

InvalidPassException(String message) { 
    super(message); 
} 
InvalidPassException(String message, Throwable cause) { 
    super(message, cause); 
} 

}

}

source

Problemi correlati