我有一小段代碼在某些要處理的事務中運行。每筆交易都標有一個交易號碼,該號碼由外部程序生成,不一定按順序排列。當我在處理代碼中捕獲一個異常時,我將它扔到主類並記錄下來以供日後查看。我想將交易號添加到此拋出的異常。是否可以在保持正確的堆棧跟蹤的同時執行此操作?將自定義消息添加到拋出的異常,同時在Java中維護堆棧跟蹤
例如:
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;
}
}
}
這正是我一直在尋找。謝謝。 – thedan