代码如下~
class ExceptionOne extends Exception{
ExceptionOne(){
System.out.println("结果不是偶数……");
}
}
class ExceptionTwo extends Exception{
ExceptionTwo(){
System.out.println("结果是本身……");
}
}
class CustomException{
protected CustomException(){
}
public static void main(String[] args){
int [] arr1={4,8,15,32,64,127,256,512};
int [] arr2={2,1,2,4,4,4,8};
int result;
try{
for(int i=0;i<arr1.length;i++){
result=arr1[i]/arr2[i];
if((result%2)!=0){
throw new ExceptionOne();
}else if(arr1[i]==result){
throw new ExceptionTwo();
}
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组越界……"+e);
}
}
}
图我不会贴……
class ExceptionOne extends Exception{
ExceptionOne(){
System.out.println("结果不是偶数……");
}
}
class ExceptionTwo extends Exception{
ExceptionTwo(){
System.out.println("结果是本身……");
}
}
class CustomException{
protected CustomException(){
}
public static void main(String[] args){
int [] arr1={4,8,15,32,64,127,256,512};
int [] arr2={2,1,2,4,4,4,8};
int result;
try{
for(int i=0;i<arr1.length;i++){
result=arr1[i]/arr2[i];
if((result%2)!=0){
throw new ExceptionOne();
}else if(arr1[i]==result){
throw new ExceptionTwo();
}
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组越界……"+e);
}
}
}
图我不会贴……
}else if(arr1[i]==result){
throw new ExceptionTwo(); //这里也会抛出异常
} LZ没有对这两个异常进行处理,要么在try后面捕获,要么在可能抛出异常的地方用throws 进行声明
我是想在FOR循环里就捕获,放在TRY块后,那不是等循环完了才开始捕获么?
我以前做过一道题,就是先自定义一个异常,然后在主方法里的TRY块里,当达到某个条件时,就用THROW抛出自定义异常。
这道题,在主方法的TRY块里,如果在循环时,当结果不是偶数或者除数是本身时就应当抛出异常的啊。
但编译不了源文件,就是说这两个THROW有错误~
改成public static void main(String[] args) throws Exception {