Posted on 2005-09-06 10:37
锋出磨砺 阅读(685)
评论(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","严重错误,适配器无效","因为适配器所在前置机网络异常,造成适配器无效"));
}
public ErrorInfo getErrorInfo(Object errorCode)
{
return (ErrorInfo)errorMap.get(errorCode);
}
}
异常实现
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();
}
}
}