class FuShuIndexException extends Exception{
FuShuIndexException(){}
FuShuIndexException(String msg){
super(msg);
}
}
class Demo{
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];
Demo d = new Demo();
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("负数角标异常!!!!");
}
}
}
|
|