Java异常机制、Java异常的捕获顺序、Throw和Throws的区别、异常中的return

综合网上各类博客总结出来,只供学习。

Java异常的捕获顺序

         package com.view.exception;

 

public class TestMoreCatch {

   public static void main(String[] args) {

      int a = 6;

      int b = 0;

      try { // try监控区域

        if (b == 0)

           throw new ArithmeticException(); // 通过throw语句抛出异常

        System.out.println("a/b的值是:" + a / b);

      } catch (ArithmeticException e) { // catch捕捉异常

        System.out.println("程序出现异常,变量b不能为0");//运行

      } catch (Exception e) {

        /**

         * 范围更大的Exception不但必须放在后面

         * 并且放在后面还不会被运行(被前面的范围更小的

         * 异常拦截了)

         */

        System.out.println("执行exception 父类的异常");

      }

      System.out.println("程序正常结束。");

   }

}

 

/*

 * 要点1:尽管ArithmeticException继承自Exception。可是当发生ArithmeticException异常

 * 并捕获的时候,就仅仅会捕获实际发生的这个异常。并不会由于Exception是其父类而

 *

 * 运行Exception那个catch子句。

 *

 * 要点2:可是假设你尝试将范围更大的Exceptioncatch语句放到的catch语句的前面,那么就会发生

 *

 * catch子句不可到达的错误“Unreachablecatch block for ArithmeticException.

 *

 * Itis already handled by the catch block for Exception”

 *

 * 即范围更大的异常(父类)必须放在后面,假设没有继承关系,比方ClassNotFoundException

 *

 * ArithmeticExceptioncatch子句之间就无所谓先后关系。

 */

Throw和Throws的区别:

 

Throw

作用在方法内,表示抛出具体异常,由方法体内的语句处理。

具体向外抛出的动作,所以它抛出的是一个异常实体类。若执行了Throw一定是抛出了某种异常。

Throws

作用在方法的声明上,表示如果抛出异常,则由该方法的调用者来进行异常处理。

主要的声明这个方法会抛出会抛出某种类型的异常,让它的使用者知道捕获异常的类型。

出现异常是一种可能性,但不一定会发生异常。

实例:

 

void testException(int a) throws IOException,{

           try{

                 ......

           }catch(Exception1 e){

              throw e;

           }catch(Exception2 e){

              System.out.println("出错了!");

           }

           if(a!=b)

              throw new  Exception3("自定义异常");

}

异常两种处理方式

  1. throws        抛出异常,自己不处理
  2. try-catch     自己处理异常

抛出异常

谁调用抛给谁去解决

分成两步:

  1. 抛异常      throw new Exception(“”)
  2. 接异常      throws Exception

异常中的return

public class TestReturn {

   public static void main(String[] args) {

        System.out.println("=============NoException==================");//1

        System.out.println(NoException());//2

        System.out.println("===============================");   //3

   }

   public static int NoException(){

        int i=10;

        try{

          System.out.println("i in try block is"+i);//2.1

          return --i; //2.2 i=9

        }

        catch(Exception e){

          --i;

          System.out.println("i in catch - form try block is"+i);

          return --i;

        }

        finally{    

          System.out.println("i in finally - from try or catch block is"+i);//2.3

          return --i;//2.4 i=8

        } 

   }

}

1.try块中没有抛出异常,trycatchfinally块中都有return语句

执行顺序:
执行try块,执行到return语句时,先执行return的语句,--i,但是不返回到main方法,执行finally块,遇到finally块中的return语句,执行--i,并将值返回到main方法,这里就不会再回去返回try块中计算得到的值。
结论:try-catch-finally都有return语句时,没有异常时,返回值是finally中的return返回的

 


2.try块中没有抛出异常,仅trycatch中有return语句

执行顺序:
try中执行完return的语句后,不返回,执行finally块,finally块执行结束后,返回到try块中,返回i在try块中最后的值。
结论:try-catch都有return语句时,没有异常时,返回值是try中的return返回的。

 

public class TestReturn02 {

   public static void main(String[] args) {

      System.out.println("=============NoException==================");// 1

      System.out.println(HaveException());// 2

      System.out.println("==============================="); // 3

   }

 

   public static int HaveException() {

      int i = 10;

      try {

         System.out.println("i in try block is" + i);// 2.1

         i = i / 0;

         return --i;

      } catch (Exception e) {

         System.out.println("i in catch - form try block is" + i);// 2.2

         --i;

         System.out.println("i in catch block is" + i);// 2.3

         return --i;// 2.4

      } finally {

         System.out.println("i in finally - from try or catch block is--" + i);// 2.5

         --i;

         System.out.println("i in finally block is--" + i);// 2.6

         return --i;// 2.7

      }

   }

}


3.try块中抛出异常,trycatchfinally中都有return语句
 

执行顺序:
抛出异常后,执行catch块,在catch块的return的--i执行完后,并不直接返回而是执行finally,因finally中有return语句,所以,执行,返回结果6。
结论:try块中抛出异常,try、catch和finally中都有return语句,返回值是finally中的return。

4.try块中抛出异常,trycatch中都有return语句

执行顺序:
抛出异常后,执行catch块,执行完finally语句后,依旧返回catch中的执行return语句后的值,而不是finally中修改的值。
结论:返回的catchreturn值。

public class TestReturn03 {

   public static void main(String[] args) {

      System.out.println("=============NoException==================");// 1

      System.out.println(HaveException03());// 2

      System.out.println("==============================="); // 3

   }

 

   public static int HaveException03() {

      int i = 10;

      try {

         System.out.println("i in try block is" + i);//2.1

         i = i / 0;

         return --i;

      } catch (Exception e) {

         System.out.println("i in catch - form try block is" + i);//2.2

         int j = i / 0;

         return --i;

      } finally {

         System.out.println("i in finally - from try or catch block is" + i);//2.3

         --i//2.4

         System.out.println("i in finally block is" + i);//2.5

         return --i;     //2.6

      }

   }

}

5.trycatch中都出现异常,在finally中有返回

执行顺序:
try块中出现异常到catchcatch中出现异常到finallyfinally中执行到return语句返回,不检查异常。
结论:返回finallyreturn值。

 

6.只在函数最后出现return语句

结论:返回函数最后的return语句

总体结论:
结论一:
return语句并不是函数的最终出口,如果有finally语句,这在return之后还会执行finally(return的值会暂存在栈里面,等待finally执行后再返回)
结论二:
finally里面不建议放return语句,根据需要,return语句可以放在try和catch里面和函数的最后。可行的做法有四:
(1)return语句只在函数最后出现一次。
(2)return语句仅在try和catch里面都出现。
(3)return语句仅在try和函数的最后都出现。
(4)return语句仅在catch和函数的最后都出现。
注意,除此之外的其他做法都是不可行的,编译器会报错。

发布了46 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39038793/article/details/102987260