异常处理

class ArraySizeException extends NegativeArraySizeException
{
 ArraySizeException()
 {
  super("你传递的是非法数组大小!");
 }
}

class UserExceptionDemo
{
 int size,array[];
 UserExceptionDemo(int s)
 {
  size=s;
  try
  {
   checkSize();
  }
  catch(ArraySizeException e)
  {
   System.out.println(e);
  }
 }
 
 void checkSize() throws ArraySizeException
 {
  if(size<0)
   throw new ArraySizeException();
  
  array=new int[size];
  for(int i=0;i<size;i++)
  {
   array[i]=i+1;
   System.out.print(array[i]+" ");
  }
 }
 
 public static void main(String args[])
 {
  new UserExceptionDemo(Integer.parseInt(args[0]));
 }
}

posted @ 2009-04-19 17:03  kanjc  阅读(159)  评论(0编辑  收藏  举报