Posted on 2005-09-06 10:37
鋒出磨礪 閱讀(691)
評論(0) 編輯 收藏 所屬分類:
java算法
抽象類 繼承于 Exception
public abstract class AbstractException extends Exception{
private ErrorInfo info;
public AbstractException(ErrorInfo message) {
super(message.getErrorCode()+message.getErrorName()+message.getErrorInfo());
info = message;
}
public String getCode()
{
return info.getErrorCode();
}
}
錯誤實體
public class ErrorInfo {
private String ErrorCode;
private String ErrorName;
private String ErrorInfo;
public ErrorInfo(String temp1,String temp2,String temp3) {
this.ErrorCode = temp1;
this.ErrorName = temp2;
this.ErrorInfo = temp3;
}
public String getErrorCode()
{
return this.ErrorCode;
}
public String getErrorName()
{
return this.ErrorName;
}
public String getErrorInfo()
{
return this.ErrorInfo;
}
}
錯誤集合
public class ErrorPool {
private java.util.HashMap errorMap = new java.util.HashMap();
public ErrorPool() {
errorMap.put("Center1001",new ErrorInfo("Center1001","嚴(yán)重錯誤,適配器無效","因為適配器所在前置機(jī)網(wǎng)絡(luò)異常,造成適配器無效"));
}
public ErrorInfo getErrorInfo(Object errorCode)
{
return (ErrorInfo)errorMap.get(errorCode);
}
}
異常實現(xiàn)
public class TestException extends AbstractException {
private ErrorInfo info;
public TestException(ErrorInfo message)
{
super(message);
info = message;
};
public String getCode()
{
return super.getCode();
}
public void LogDebug()
{
System.out.println("debug info.....");
}
}
具體使用代碼
public class Test {
public Test() {
}
public void kk(String usename) throws TestException
{
if (usename.equals("fuck"))
{
}
else
{
throw(new TestException((new ErrorPool()).getErrorInfo("Center1001")));
}
}
public static void main(String[] agrgs)
{
try
{
Test tt = new Test();
tt.kk("xxx");
}
catch(TestException e)
{
System.out.println(e.getCode());
e.printStackTrace();
e.LogDebug();
}
}
}