随笔 - 217  文章 - 4  评论 - 4  阅读 - 23587

Java学习--异常

根据课上的课件进行练习

1、阅读以下代码(CatchWho.java),写出程序运行结果:

复制代码
 1 package exception;
 2 
 3 public class CatchWho {
 4     public static void main(String[] args) {
 5         try {
 6             try {
 7                 throw new ArrayIndexOutOfBoundsException();
 8             }
 9             catch(ArrayIndexOutOfBoundsException e) {
10                 System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");
11             }
12 
13             throw new ArithmeticException();
14         }
15         catch(ArithmeticException e) {
16             System.out.println("发生ArithmeticException");
17         }
18         catch(ArrayIndexOutOfBoundsException e) {
19             System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");
20         }
21     }
22 }
复制代码

结果:

ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException

2、写出CatchWho2.java程序运行的结果

复制代码
 1 package exception;
 2 
 3 public class CatchWho2 {
 4     public static void main(String[] args) {
 5         try {
 6             try {
 7                 throw new ArrayIndexOutOfBoundsException();
 8             }
 9             catch(ArithmeticException e) {
10                 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
11             }
12             throw new ArithmeticException();
13         }
14         catch(ArithmeticException e) {
15             System.out.println("发生ArithmeticException");
16         }
17         catch(ArrayIndexOutOfBoundsException e) {
18             System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
19         }
20     }
21 }
复制代码

结果:ArrayIndexOutOfBoundsException/外层try-catch

3、阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结

复制代码
 1 package exception;
 2 
 3 
 4 class EmbededFinally {
 5     public static void main(String args[]) {
 6 
 7         int result;
 8 
 9         try {
10 
11             System.out.println("in Level 1");
12 
13 
14             try {
15 
16                 System.out.println("in Level 2");
17                 // result=100/0;  //Level 2
18 
19                 try {
20 
21                     System.out.println("in Level 3");
22 
23                     result=100/0;  //Level 3
24 
25                 }
26 
27                 catch (Exception e) {
28 
29                     System.out.println("Level 3:" + e.getClass().toString());
30 
31                 }
32 
33 
34                 finally {
35 
36                     System.out.println("In Level 3 finally");
37 
38                 }
39 
40 
41                 // result=100/0;  //Level 2
42 
43 
44             }
45 
46             catch (Exception e) {
47 
48                 System.out.println("Level 2:" + e.getClass().toString());
49 
50             }
51             finally {
52 
53                 System.out.println("In Level 2 finally");
54 
55             }
56 
57             // result = 100 / 0;  //level 1
58 
59         }
60 
61         catch (Exception e) {
62 
63             System.out.println("Level 1:" + e.getClass().toString());
64 
65         }
66 
67         finally {
68 
69              System.out.println("In Level 1 finally");
70 
71         }
72 
73     }
74 
75 }
复制代码

结果:

in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally

try捕捉到的异常会从一到二进行抛出,finally的异常会从二到一抛出。

4、finally语句块一定会执行吗?

复制代码
 1 public class SystemExitAndFinally {
 2 
 3     
 4     public static void main(String[] args)
 5     {
 6         
 7         try{
 8 
 9             
10             System.out.println("in main");
11             
12             throw new Exception("Exception is thrown in main");
13 
14                     //System.exit(0);
15 
16         
17         }
18         
19         catch(Exception e)
20 
21             {
22             
23             System.out.println(e.getMessage());
24             
25             System.exit(0);
26         
27         }
28         
29         finally
30         
31         {
32             
33             System.out.println("in finally");
34         
35         }
36     
37     }
38 
39 
40 }
复制代码

结果:

in main
Exception is thrown in main

finally不一定会执行。

当程序执行try块,catch块时遇到return语句或者throw语句,这两个语句都会导致该方法立即结束,所以系统并不会立即执行这两个语句,而是 去寻找该异常处理流程中的finally块,如果没有finally块,程序立即执行return语句或者throw语句,方法终止。如果有 finally块,系统立即开始执行finally块,只有当finally块执行完成后,系统才会再次跳回来执行try块、catch块里的 return或throw语句,如果finally块里也使用了return或throw等导致方法终止的语句,则finally块已经终止了方法,不用 再跳回去执行try块、catch块里的任何代码了。

综上:尽量避免在finally块里使用return或throw等导致方法终止的语句,否则可能出现一些很奇怪的情况!

posted on   跨越&尘世  阅读(35)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示