07-异常处理 完成课件中的动手动脑的或需要验证的相关内容

一、请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。

  1、程序源代码

 1 import javax.swing.*;
 2 
 3 class AboutException {
 4    public static void main(String[] a) 
 5    {
 6       int i=1, j=0, k;
 7       k=i/j;
 8 
 9 
10     try
11     {
12         
13         k = i/j;    // Causes division-by-zero exception
14         //throw new Exception("Hello.Exception!");
15     }
16     
17     catch ( ArithmeticException e)
18     {
19         System.out.println("被0除.  "+ e.getMessage());
20     }
21     
22     catch (Exception e)
23     {
24         if (e instanceof ArithmeticException)
25             System.out.println("被0除");
26         else
27         {  
28             System.out.println(e.getMessage());
29             
30         }
31     }
32 
33     
34     finally
35      {
36              JOptionPane.showConfirmDialog(null,"OK");
37      }
38         
39   }
40 }

2、运行结果截图

二、多层的异常捕获-1  

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

运行结果截图:

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

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

运行结果截图:

四、请先阅读 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 }

运行结果截图:

 

当有多层嵌套的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 }

运行结果截图:

总结:
(1)try语句没有被执行到,如在try语句之前return就返回了,这样finally语句就不会执行。这也说明了finally语句被执行的必要而非充分条件是:相应的try语句一定被执行到。

(2)在try块|catch块中有System.exit(0);这样的语句。System.exit(0)是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。

在try-catch-finally中, 当return遇到finally,return对finally无效,即:

     1.在try catch块里return的时候,finally也会被执行。

     2.finally里的return语句会把try catch块里的return语句效果给覆盖掉。

结论:return语句并不一定都是函数的出口,执行return时,只是把return后面的值复制了一份到返回值变量里去了。(总结部分来源于网络)

 验证:

由图可知不能通过编译

但是修正代码后得以编译

因为 throws语句表明某种方法可能出现某种异常,而它自己不能处理,而需要由调用者来处理。

六、编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

程序源代码:

 1 import java.util.InputMismatchException;
 2 import java.util.Scanner;
 3 
 4 public class Test {
 5       @SuppressWarnings("resource")
 6     public static void main(String[]a) {
 7           int num = 0;
 8           Scanner input = new Scanner(System.in);
 9          
10               try {
11                   System.out.print("输入数学分数:");
12                   num = input.nextInt();      
13                   if(num<0||num>100)throw new NumberFormatException();
14               }
15               catch(NumberFormatException e) {
16                   //如果输入的是越界数字,直接抛出这个格式异常
17                   System.out.println("输入有误!");
18                   input.close();
19                   System.exit(-1);
20               }
21               catch(InputMismatchException e) {
22                   //如果输入的不是整形,而是double或者String等类型,会直接抛出这个异常
23                   System.out.println("输入有误!");
24                   input.close();
25                   System.exit(-1);
26               }
27           if(num<60)System.out.println("数学不及格");
28           else if(num>=60&&num<=69)System.out.println("数学及格");
29           else if(num>=70&&num<=79)System.out.println("数学中等");
30           else if(num>=80&&num<=89)System.out.println("数学良好");
31           else if(num>=90&&num<=99)System.out.println("数学优秀");
32           else System.out.println("数学满分");
33           input.close();
34       }
35 }

2、运行结果截图

 

posted @ 2017-11-15 17:33  野生小码农  阅读(182)  评论(0)    收藏  举报