日志文章

2007年12月23日 17:09:04

Exception处理

定义一个异常类方法:

public class APCException extends Exception
{
private static final long serialVersionUID = 1L;

public static final String APC_EXCEPTION_STOCK_ID_ERROR       = "001001";   //输入条件中ID有错误
public static final String APC_EXCEPTION_STOCK_DATE_ERROR     = "001002";   //输入的日期不是成对出现
public static final String APC_EXCEPTION_STOCK_DATE_CHECK_ERROR = "001003";   //输入的日期字符串不符合实际要求
public static final String APC_EXCEPTION_STOCK_STRING_ERROR   = "001004";   //输入的字符串为空或为NULL

String error = "";
public APCException(String error)
{
this.error = error;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}


使用异常类

public class Test
{
//验证参数
private static boolean checkParameter() throws APCException
{
  throw new APCException(APCException.APC_EXCEPTION_STOCK_DATE_ERROR);
}
public static void main(String[] args)
{
String error = "";
try
{
  checkParameter();
}
catch (APCException e)
{
  error = e.getError();
}
System.out.println(error);
}
}

Tags: 异常   Exception  

类别: 无分类 |  评论(1) |  浏览(7570) |  收藏
发表评论