如何在java中形成多个类的异常原因和消息

时间:2015-02-09 14:47:19

标签: java exception-handling

我开发了一个Servlet,processReq()调用One Class的方法,One类调用Two Class的metodo .....直到第五类,我知道的是什么,如果在任何方法中发生任何异常,我必须在DataBase中保存StackTrace。为了实现这一点,我已经编写了以下代码,它工作正常。任何人都可以建议我这是正确的方法还是有更好的方法?请建议我。任何建议将不胜感激 注意:出于测试目的,我们使用main()方法

编写
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;
}


}

主程序(这将是servlet)

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 个答案:

答案 0 :(得分: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;
}

(我没有运行它,所以它可能不起作用,但你明白了)

确保该文本栏中有足够的空间!!