JAVA异常题目总结

java异常题总结

1.运行下面的程序,结果一般会输出_14_
 public class J_test{
    public static void main(String[] args) {
         try {
               System.out.println("1");
               if(true){
                        return;      //return语句不会阻止finally语句的执行,但是会阻止finally后面的语句的执行
               }
        }catch(RuntimeException e) {
              System.out.println("2");
              return;
       }finally{
               System.out.println("4");
       }
         System.out.println("End!"); 
    }
 }
2.运行下面的程序,结果一般会输出_1_
 public class J_test{
    public static void main(String[] args) {
          try {
                  System.out.println("1");
                  if(true){
                          System.exit(0);     //System.exit(0)语句会阻止一切语句的执行,是终止程序的意思
                  }
          }catch(RuntimeException e) {
                   System.out.println("2");
                   return;
          }finally{
                   System.out.println("4");
          }
                   System.out.println("End!"); 
    }
 }
3.运行下面的程序,结果一般会输出_2:0,3:4,4:4,5:4_
public class J_test{
    public static void main(String[] args) {
         int i = 0;
         try {
                for (i = 0; i < 10; i++) {
                       try {
                               if(i%3 == 0){
                                       throw new Exception(); //抛出异常,马上捕获,所以不会打印1:1;
                               }
                               System.out.println("1:"+1+",");
                       }catch(Exception e) {
                               System.out.println("2:"+i+",");
                               i += 2;
                               if(i%3 == 2){
                                       throw new Exception(); //再次抛出异常,不会执行retrun语句
                               }
                               return;
                      }finally{                     //在捕获异常之前不需执行的
                               i *= 2;
                              System.out.println("3:"+i+",");
                      }
                }
          }catch(Exception e){
                     System.out.println("4:"+i+",");
                     return;   //该return语句阻止finally后面的语句的执行
          }finally{
                     System.out.println("5:"+i+",");  
          }
                     System.out.println("End!");
       }
}
4.参照下面的类间的继承关系图,写出下面程序的输出是_c_m_n_
     java.lang.Object
      |__java.lang.Throwable
          |__java.lang.Error
          |__java.lang.Exception
              |__java.io.IOException
              |__java.lang.RuntimeException
                  |__java.lang.ArithmeticException
                  |__java.lang.IndexOutOfBoundException
                      |__java.lang.ArrayIndexOutOfBoundsException

public class J_test{
      public static void main(String[] args) {
               try {
                        mb_method();
               }catch(Exception e){
                        System.out.println('m');   //该异常能捕获ArrayIndexOutOfBoundsException,所以m会打印
               }
                        System.out.println('n');   //毫无疑问n当然也会被打印
      }
      static void mb_createException(){
                throw new ArrayIndexOutOfBoundsException();
      }
      static void mb_method(){
                try {
                        mb_createException();
                        System.out.println("a");
                }catch(ArithmeticException e){  //由于ArithmeticException不能捕获ArrayIndexOutOfBoundsException
                        System.out.println("b");   //所以不能打印b,但是由于try-catch-finally是捕获块,无论如何也会执行的
                }finally{
                            System.out.println("c");   //所以c会被打印
                }
                   System.out.println("d");   //由于上述异常未被捕获,所以d不会被打印,马上在上面进行捕获
        }
}
5.参照下面的类间的继承关系图,写出下面程序的输出是_b_b_d_n_
     java.lang.Object                        //此题是上题的拓展
      |__java.lang.Throwable
          |__java.lang.Error
          |__java.lang.Exception
              |__java.io.IOException
              |__java.lang.RuntimeException
                  |__java.lang.ArithmeticException
                  |__java.lang.IndexOutOfBoundException
                      |__java.lang.ArrayIndexOutOfBoundsException

public class J_test{
        public static void main(String[] args) {
                try {
                         mb_method();
                }catch(Exception e){        //该异常能捕获ArrayIndexOutOfBoundsException,但是
                         System.out.println('m'); //ArrayIndexOutOfBoundsException异常在下面已被捕获,所以m不会被打印
                }
                         System.out.println('n');   //毫无疑问n当然也会被打印
        }
        static void mb_createException(){
                 throw new ArrayIndexOutOfBoundsException();
        }
        static void mb_method(){
                  try {
                            mb_createException();
                            System.out.println("a");
                            }catch(Exception e){       //由于Exception能捕获ArrayIndexOutOfBoundsException
                            System.out.println("b");   //所以能打印b,但是由于try-catch-finally是捕获块,无论如何也会执行的
                  }finally{
                        System.out.println("c");   //所以c会被打印
                  }
                        System.out.println("d");   //由于上述异常已被捕获,所以d会被打印,在上面当然就不会进行捕获了
         }
}
6.下面程序输出的是_13423_
public class J_test{
          public static void mb_method(int i){
                   try {
                          if(i ==1){
                          throw new Exception();
                          }
                          System.out.println("1");
                   }catch(Exception e){
                          System.out.println("2");
                          return;
                   }finally{
                           System.out.println("3");
                   }
                       System.out.println("4"); 
           }
     public static void main(String[] args) {
               mb_method(0); //该方法不会抛异常,所以直接打印134
               mb_method(1); //该方法抛异常,1不会打印,2打印后,执行return语句,所以3打印后,4不会打印
     }
}
好了,经过对这几个题的思考,相信大家对异常题有很大一部分的了解了吧,不过就那么几个简单的特性,下面
让我们一起来总结一下吧:

    异常的特点:
       (1)try-catch-finally语句块无论如何都会执行完,这句话毫无疑问,上面好多题鉴证了的;
       (2)try-catch-finally语句块中要是出现了return语句,则会阻止finally后面的语句的执行,如上面的 1、3、6题;
       (3)try-catch-finally语句块中要是出现了System.exit(0)语句,则会阻止这条语句后面的所有语句的执行,如2题;
       (4)在捕获异常的时候,若异常类Exception1是异常类Exception2的父类,则可用Exception1去捕获该异常,如上面的4、5题;
       (5)在捕获异常的时候,需要遵循由具体到抽象的原则,就是说,如果一个异常语句同时抛出两个异常,抛出的这两个异常若存在父子关系则需要先用子类去捕获,再用父类去捕获,如上面的第5题,ArrayIndexOutOfBoundsException本是Exception的子类,所以可以用它来捕获该异常;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值