打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java 异常捕捉 ( 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

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 了 *_*






   [ 随笔均原创,转载请注明出处:http://www.blogjava.net/fancydeepin ]

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
关于java异常处理的几个关键字 try catch/throw/throws
try/catch/finally测试
03语法总结 - 异常
Java中的异常、断言、日志【草稿上】
开发项目中碰见异常情况怎么办?
《Java Web应用程序开发》01 异常
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服