try-catch-finally

前言:
java 中的异常处理机制你真的理解了吗?掌握了吗?
catch 体里遇到 return 是怎么处理? finally 体遇到 return 怎么办?finally 体里有 System.exit() 方法怎么处理?当 catch 和 finally 体里同时遇上 return 怎么办?

相信你在处理异常的时候不是每次都把它 throws 掉就完事了,很多时候异常是需要我们自己来 catch 并针对所抛出的 Exception 做一些后续的处理工作。

直接上代码,先贴下面测试需要调用的方法:
 1
 2     //  catch 后续处理工作
 3      public  static  boolean catchMethod()  {
 4        System.out.print("call catchMethod and return  --->>  ");
 5        return false;
 6    }

 7      //  finally后续处理工作
 8      public  static  void finallyMethod()  {
 9        System.out.println();
10        System.out.print("call finallyMethod and do something  --->>  ");
11    }

12


1. 抛出 Exception,没有 finally,当 catch 遇上 return
 1
 2 public  static  boolean catchTest()  {
 3        try {
 4            int i = 10 / 0;   // 抛出 Exception,后续处理被拒绝
 5            System.out.println("i vaule is : " + i);
 6            return true;    // Exception 已经抛出,没有获得被执行的机会
 7        }
 catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return catchMethod();    // Exception 抛出,获得了调用方法并返回方法值的机会
10        }

11    }

12

后台输出结果:
1
2  -- Exception --
3 call catchMethod and  return  --->>   false
4

2. 抛出 Exception,当 catch 体里有 return,finally 体的代码块将在 catch 执行 return 之前被执行
 1
 2 public  static  boolean catchFinallyTest1()  {
 3        try {
 4            int i = 10 / 0; // 抛出 Exception,后续处理被拒绝
 5            System.out.println("i vaule is : " + i);
 6            return true;   // Exception 已经抛出,没有获得被执行的机会
 7        }
 catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return catchMethod();  // Exception 抛出,获得了调用方法的机会,但方法值在 finally 执行完后才返回
10        }
finally{
11            finallyMethod();  // Exception 抛出,finally 代码块将在 catch 执行 return 之前被执行
12        }

13    }

14

后台输出结果:
1
2  -- Exception --
3 call catchMethod and  return  --->>  
4 call finallyMethod and  do something  --->>   false
5


2‘. 抛出Exception,当catch里面有throw e, finally体的代码块将在catch执行throw之前被执行。(经过测试)


3. 不抛 Exception,当 finally 代码块里面遇上 return,finally 执行完后将结束整个方法
 1
 2 public  static  boolean catchFinallyTest2()  {
 3        try {
 4            int i = 10 / 2;  // 不抛出 Exception
 5            System.out.println("i vaule is : " + i);
 6            return true;   // 获得被执行的机会,但执行需要在 finally 执行完成之后才能被执行
 7        }
 catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return catchMethod();
10        }
finally{
11            finallyMethod();
12            return false// finally 中含有 return 语句,这个 return 将结束这个方法,不会在执行完之后再跳回 try 或 catch 继续执行,方法到此结束,返回 false
13        }

14    }

15

后台输出结果:
1
2 i vaule is : 5
3
4 call finallyMethod and  do something  --->>   false
5

4. 不抛 Exception,当 finally 代码块里面遇上 System.exit() 方法 将结束和终止整个程序,而不只是方法
 1
 2 public  static  boolean finallyExitTest()  {
 3        try {
 4            int i = 10 / 2;  // 不抛出 Exception
 5            System.out.println("i vaule is : " + i);
 6            return true;   // 获得被执行的机会,但由于 finally 已经终止程序,返回值没有机会被返回
 7        }
 catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return true;
10        }
finally {
11            finallyMethod();
12            System.exit(0);// finally 中含有 System.exit() 语句,System.exit() 将退出整个程序,程序将被终止
13        }

14    }

15

后台输出结果:
1
2 i vaule is : 5
3
4 call finallyMethod and  do something  --->>  
5

5. 抛出 Exception,当 catch 和 finally 同时遇上 return,catch 的 return 返回值将不会被返回,finally 的 return 语句将结束整个方法并返回
 1
 2 public  static  boolean finallyTest1()  {
 3        try {
 4            int i = 10 / 0; // 抛出 Exception,后续处理被拒绝
 5            System.out.println("i vaule is : " + i);
 6            return true;   // Exception 已经抛出,没有获得被执行的机会
 7        }
 catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return true;  // Exception 已经抛出,获得被执行的机会,但返回操作将被 finally 截断
10        }
finally {
11            finallyMethod();
12            return false;  // return 将结束整个方法,返回 false
13        }

14    }

15

后台输出结果:
1
2  -- Exception --
3
4 call finallyMethod and  do something  --->>   false
5

6. 不抛出 Exception,当 finally 遇上 return,try 的 return 返回值将不会被返回,finally 的 return 语句将结束整个方法并返回
 1
 2 public  static  boolean finallyTest2()  {
 3        try {
 4            int i = 10 / 2;  // 不抛出 Exception
 5            System.out.println("i vaule is : " + i);
 6            return true;   // 获得被执行的机会,但返回将被 finally 截断
 7        }
 catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return true;
10        }
finally {
11            finallyMethod();
12            return false// return 将结束这个方法,不会在执行完之后再跳回 try 或 catch 继续执行,返回 false
13        }

14    }

15

后台输出结果:
1
2 i vaule is : 5
3
4 call finallyMethod and  do something  --->>   false
5
结语:
(假设方法需要返回值)
java 的异常处理中,
在不抛出异常的情况下,程序执行完 try 里面的代码块之后,该方法并不会立即结束,而是继续试图去寻找该方法有没有 finally 的代码块,
如果没有 finally 代码块,整个方法在执行完 try 代码块后返回相应的值来结束整个方法;
如果有 finally 代码块,此时程序执行到 try 代码块里的 return 语句之时并不会立即执行 return,而是先去执行 finally 代码块里的代码,
若 finally 代码块里没有 return 或没有能够终止程序的代码,程序将在执行完 finally 代码块代码之后再返回 try 代码块执行 return 语句来结束整个方法;
若 finally 代码块里有 return 或含有能够终止程序的代码,方法将在执行完 finally 之后被结束,不再跳回 try 代码块执行 return。

在抛出异常的情况下,原理也是和上面的一样的,你把上面说到的 try 换成 catch 去理解就 OK 了 *_*


exception有以下几个最主要的属性值:
message - the detail message.你想传递给用户的一些信息,它是一个字符串类型,你可以写任何你想写的message。
cause - the cause. (A  null value is permitted, and indicates that the cause is nonexistent or unknown.)
enableSuppression - whether or not suppression is enabled or disabled

writableStackTrace - whether or not the stack trace should be writable


The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.

Throwable是所有error和exception的父类,只有这种类型的对象实例才能被jvm的throw子句抛出,同样的也只有这样类型的子句才能够被catch子句获得。可以通过throwable来获得线程运行过程中产生的非正常错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值