课后作业

动手动脑

(1)          请阅读并运行AboutException.java示例,

源代码:

 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 }

答:Java中的异常捕获语句

             Try{

                    //可能发生运行错误的代码;

             }

             catch(异常类型     异常对象引用){

                    //用于处理异常的代码

             }

             finally{

                    //用于“善后” 的代码

             }

Java 中所有可捕获的异常都派生自 Exception 类。

把可能会发生错误的代码放进try语句块中。

当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。

             catch语句块中的代码用于处理错误。

当异常发生时,程序控制流程由try语句块跳转到catch语句块。

不管是否有异常发生,finally语句块中的语句始终保证被执行。

如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

 

Throwable类有两个直接子类:

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

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

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

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

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

         throw new ArithmeticException(…);

(2)多层的异常捕获-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 }

答:

多层的异常捕获-2

写出CatchWho2.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 }

答:

 

(3)当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。

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

答:不一定执行finally语句的内容,抛出的错误决定执行的catch语句再执行finally语句。

课后作业2动手动脑

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

要求程序必须具备足够的健壮性,不管用户输入什    么样的内容,都不会崩溃。

程序源代码:

 1 import java.util.*;
 2 public class Test9 {
 3  
 4     
 5       public static void main(String[] args)throws Exception
 6     {
 7         Scanner cin=new Scanner(System.in);
 8         System.out.println("请输入考试成绩");
 9              try{
10                     String in=cin.next();
11                     boolean flag=true;
12                     for(int i=0;i<in.length()&&flag;i++){
13                            if(in.charAt(i)<=48||in.charAt(i)>=57)
14                                   flag=false;
15                     }
16                     if(flag)
17                            throw new Exception("包含非法字符");
18                     double m=Double.parseDouble(in);
19                     if(m>=90)
20                            System.out.println("优");
21                     else if(m>=80)
22                            System.out.println("良");
23                     else if(m>=70)
24                            System.out.println("中");
25                     else if(m>=60)
26                            System.out.println("及格");
27                     else 
28                            System.out.println("不及格");
29              }
30              catch(Exception a){
31                     System.out.println(a.getMessage());
32                     System.exit(0);
33              }
34       
35       }
36  
37  
38 }

程序截图验证:

原文地址:https://www.cnblogs.com/wangfengbin/p/6102956.html