2015-02-09 51 views
0

我開發了一個Servlet,processReq()調用一個類的方法,One類調用兩個Class的metodo .....直到Class五,什麼如果在任何方法中發生任何異常,我必須在DataBase中保存StackTrace。爲了實現這一點,我寫了下面的代碼,它工作正常。任何人都可以建議我是這種正確的方式還是有更好的方法?請建議我。任何建議將不勝感激。
注:用於測試目的,我們已經寫在main()方法如何從java中獲取異常原因和消息形式多類

public class MyTestException extends Exception { 

private String stackTraceMessage; 
private String excepMsg; 
private String errorType; 
private String errorMsg; 
private Byte status; 
public boolean msgSet ; 

public MyTestException(String stackTraceMessage, String excepMsg, String errorType, String errorMsg, Byte status, boolean msgSet) { 
    this.stackTraceMessage = stackTraceMessage; 
    this.excepMsg = excepMsg; 
    this.errorType = errorType; 
    this.errorMsg = errorMsg; 
    this.status = status; 
    this.msgSet = msgSet; 
} 



public String getStackTraceMessage() { 
    return stackTraceMessage; 
} 

public void setStackTraceMessage(String stackTraceMessage) { 
    this.stackTraceMessage = stackTraceMessage; 
} 

public String getExcepMsg() { 
    return excepMsg; 
} 

public void setExcepMsg(String excepMsg) { 
    this.excepMsg = excepMsg; 
} 

public String getErrorType() { 
    return errorType; 
} 

public void setErrorType(String errorType) { 
    this.errorType = errorType; 
} 

public String getErrorMsg() { 
    return errorMsg; 
} 

public void setErrorMsg(String errorMsg) { 
    this.errorMsg = errorMsg; 
} 

public Byte getStatus() { 
    return status; 
} 

public void setStatus(Byte status) { 
    this.status = status; 
} 

public boolean isMsgSet() { 
    return msgSet; 
} 

public void setMsgSet(boolean msgSet) { 
    this.msgSet = msgSet; 
} 

} 

一類

public class One { 
public static String getMsgFromOne() throws Exception { 
    String msg = null; 
    try { 
     msg = Two.getMsgFromTwo(); 
    } catch (Exception e) { 
     if(e instanceof MyTestException){ 
     MyTestException ex = (MyTestException) e; 
      if (ex.isMsgSet()) { 
      throw e; 
     } else { 
      throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 
     }else{ 
       throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 
    } 
    return msg; 
} 
} 

二班

public class Two { 
public static String getMsgFromTwo()throws Exception{ 
String msg = null; 
    try { 
     msg = Three.getMsgFromThree(); 
//   int a = 9/0; 
    } catch (Exception e) { 
     if(e instanceof MyTestException){ 
     MyTestException ex = (MyTestException) e; 
      if (ex.isMsgSet()) { 
      throw e; 
     } else { 
      throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 
     }else{ 
       throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 
    } 
return msg; 
} 
} 

三班

public class Three { 
public static String getMsgFromThree()throws Exception{ 
String msg = null; 
    try { 
     msg = Four.getMsgFromTwo(); 
    } catch (Exception e) { 
     if(e instanceof MyTestException){ 
     MyTestException ex = (MyTestException) e; 
      if (ex.isMsgSet()) { 
      throw e; 
     } else { 
      throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 
     }else{ 
       throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 

    } 
return msg; 
} 
} 

四大類

public class Four { 
    public static String getMsgFromTwo() throws Exception{ 
String msg = null; 
    try { 
     msg = Five.getMsgFromFive(); 
//   int a=0; 
//  a= 8/0; 
//   if(a==0) 
//   throw new RemoteException("~~~~~~~~~ Rm Exp"); 
    } catch (Exception e) { 
     if(e instanceof MyTestException){ 
     MyTestException ex = (MyTestException) e; 
      if (ex.isMsgSet()) { 
      throw e; 
     } else { 
      throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 
     }else{ 
       throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 
    } 
return msg; 
} 
} 

五大類

public class Five { 

public static String getMsgFromFive() throws Exception { 
    String msg = null; 
    try { 
//   msg = "Hello from 5"; 
     int a = 9/0; 
//   if (a == 0) { 
//    throw new MyTestException("StackTrace Five", "ExcMessage", "ErrorType", "ErrMsg", new Byte("1"),false); 
//   } 
    } catch (Exception e) { 
     if(e instanceof MyTestException){ 
     MyTestException ex = (MyTestException) e; 
      if (ex.isMsgSet()) { 
      throw e; 
     } else { 
      throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 
     }else{ 
       throw new MyTestException(TestExp.getStackTraceMessage(e), e.getLocalizedMessage(), "ErrorType", "ErrMsg", new Byte("1"),true); 
     } 
    } 
    return msg; 
} 


} 

主程序(這將是小服務程序)

public class TestExp { 

/** 
* 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    try { 
     One.getMsgFromOne(); 
    } catch (Exception ex) { 
     // Saving all these Values in Database Table 
     MyTestException e = (MyTestException) ex; 
     System.out.println("1."+e.getErrorMsg()); 
     System.out.println("2."+e.getErrorType()); 
     System.out.println("3."+e.getStackTraceMessage()); 
     System.out.println("4."+e.getStatus()); 
     System.out.println("5."+e.getExcepMsg()); 

    } 
} 

public static String getStackTraceMessage(Exception e) { 

    StackTraceElement[] stackTraceArray = e.getStackTrace(); 
    String strStackTrace = "" + e; 
    for (StackTraceElement stackTraceElement : stackTraceArray) { 
     strStackTrace += " \n Class Name :- " + stackTraceElement.getClassName() + ", Method Name :- " + stackTraceElement.getMethodName() + " , Line Number :- " + stackTraceElement.getLineNumber(); 

    } 
    return strStackTrace; 

} 
} 

我評論過一些線測試目的
請給我建議。任何建議將不勝感激。

回答

1

一個建議,你可能也想事業添加到您的消息,例如...

public static String getStackTraceMessage(Exception e) { 

    String strStackTrace = ""; 

    while(e != null) { 
     StackTraceElement[] stackTraceArray = e.getStackTrace(); 
     strStackTrace = "" + e; 
     for (StackTraceElement stackTraceElement : stackTraceArray) { 
      strStackTrace += " \n Class Name :- " + stackTraceElement.getClassName() + ", Method Name :- " + stackTraceElement.getMethodName() + " , Line Number :- " + stackTraceElement.getLineNumber(); 

     } 

     if(e == e.getCause()) { 
      e = null; 
     } else { 
      e = e.getCause(); 
      strStackTrace += "\nCaused by "; 
     } 
    } 
    return strStackTrace; 
} 

(我沒有運行此所以它可能無法正常工作,但你的想法)

確保您在該文本欄中有足夠的空間!

+0

另一件事,爲什麼要打擾你的「MyTestException」?只需抓住所有的servlet? – BretC 2015-02-09 15:19:51

+0

你能不能在這個「一個......五個」類中,不要費心去嘗試...趕上所有的事情。「你爲什麼要打擾你的」MyTestException「?只需要抓住所有的servlet?」 – LMK 2015-02-09 15:44:04

+0

@LMK只要讓任何異常引發到你的「main」中的try ... catch塊,然後你可以在那裏創建你的「MyTestException」類型的東西 – BretC 2015-02-09 15:59:06