第八周动手动脑(2018.11.5-11.11)

动手动脑一:

请阅读并运行AboutException.java示例

AboutException.java

 答:

结论:

异常 Exception):发生于程序执行期间,表明出现了一个非法的运行状况。许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象。

例如:数组越界和被0

 动手动脑二:多层的异常捕获-1

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

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

答:运行结果:

ArrayIndexOutOfBoundsException/内层try-catch

发生ArithmeticException

动手动脑二:多层的异常捕获-2

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

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

答:运行结果:

ArrayIndexOutOfBoundsException/外层try-catch

动手动脑三:

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

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

答:运行结果:

异常清除之前:

Exception in thread "main" java.lang.Error: 无法解析的编译问题:

标记“.”上有语法错误,删除此标记

at ceshi.CatchWho2.main(CatchWho2.java:70)

异常清除之后:

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

总结::当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句块执行顺序

动手动脑三:

辨析:finally语句块一定会执行吗?

请通过 SystemExitAndFinally.java示例程序回答上述问题

 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 }
SystemExitAndFinally

答:运行结果:

in main

Exception is thrown in main

结论:

1.只有与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块才会执行的前提条件

2.如果 try 语句块中执行了 System.exit (0) 语句,终止了 Java 虚拟机的运行finally 语句块也不可以执行

3.当一个线程在执行 try 语句块或者 catch 语句块时被打断(interrupted)或者被终止(killed),与其相对应的 finally 语句块可能不会执行。

4.更极端的情况,就是在线程运行 try 语句块或者 catch 语句块时,突然死机或者断电,finally 语句块肯定不会执行了。

知识点:如何跟踪异常的传播路径?

1.当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。

2.可使用printStackTrace 和 getMessage方法了解异常发生的情况:

3.printStackTrace:打印方法调用堆栈。

4.每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。

归纳与总结

依据对本讲多个示例程序的分析,请自行归纳总结出Java多层嵌套异常处理的基本流程。

答:

Java中异常分类:

Throwable类有两个直接子类:

Exception:出现的问题是可以被捕获的;

Error:系统错误,通常由JVM处理。

可捕获的异常又可以分为两类:

1Check异常:直接派生自Exception的异常类,必须被捕获或再次声明抛出

2Runtime异常:派生自RuntimeException的异常类。使用throw语句可以随时抛出这种异常对象:

   throw new ArithmeticException(…);

处理流程:

1.try语句嵌套从外层到内层执行,在try语句中,哪一层出错,哪一层就抛出异常,该层后边的try语句就不再执行,如果该层存在catch就进行相应的捕获,且内层try中嵌套的try-catch语句外部如果还有语句会继续执行;如果该层没有catch进行捕获,就向外抛出,如果外部try-catch语句中也没有catch进行捕获,就终止程序。

2.try-catch-finally语句嵌套时,内层try抛出异常,即使catch没有捕捉到抛出的异常,内层的finally也一样会执行,然后异常继续向外抛出,除非遇到极特殊的System.exit(0)在finally语句之前的try-catch中,finally语句才不执行。

原文地址:https://www.cnblogs.com/yeshenfeng/p/9930216.html