日期:2014-05-20  浏览次数:20705 次

刚学到异常,请教一下
在自定义类继承Exception后,为什么还是说找不到这个类
package roy;

class FuShuIndexException extends Exception
{
FuShuIndexException()
{}


FuShuIndexException(String msg)
{
super(msg);
}
}


class Free
{
public int method(int[] arr,int index)throws NullPointerException//,FuShuIndexException
{
if(arr==null)
throw new NullPointerException("没有任何数组实体");
if(index<0)
throw new FuShuIndexException();

return arr[index];
}
}

class  ExceptionDemo4
{
public static void main(String[] args)
{
int[] arr = new int[3];
Free d = new Free();
try
{
int num = d.method(null,-1);
System.out.println("num="+num);

}

catch(NullPointerException e)
{
System.out.println(e.toString());
}
catch (FuShuIndexException e)
{
System.out.println("message:"+e.getMessage());
System.out.println("string:"+e.toString());

e.printStackTrace();//jvm默认的异常处理机制就是调用异常对象的这个方法。

System.out.println("负数角标异常!!!!");
}

/*
catch(Exception e)//多catch父类的catch放在最下面。
{

}
*/
System.out.println("over");
}

}

------解决方案--------------------
蹭分
------解决方案--------------------
没有 导入包吧
------解决方案--------------------
引用:
解决了,没有在方法上声明。。


方法没有throws Exception?
------解决方案--------------------
方法声明时需要throws FuShuException,因为它不是运行时异常